Coveralls logob
Coveralls logo
  • Home
  • Features
  • Pricing
  • Docs
  • Sign In

weaveworks / weave / #7201

20 Sep 2016 - 16:03 coverage decreased (-0.1%) to 73.98%
#7201

Pull #2501

circleci

7de8841b37903eead0e0bb4d567e9454?size=18&default=identiconbrb
Update weaveworks/mesh submodule
Pull Request #2501: Reject empty peers

6616 of 8943 relevant lines covered (73.98%)

121129.01 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

65.57
/proxy/create_container_interceptor.go
1
package proxy
2

3
import (
4
        "errors"
5
        "net/http"
6
        "strings"
7

8
        "github.com/fsouza/go-dockerclient"
9
)
10

11
const MaxDockerHostname = 64
12

13
var (
14
        ErrNoCommandSpecified = errors.New("No command specified")
15
)
16

17
type createContainerInterceptor struct{ proxy *Proxy }
18

19
// ErrNoSuchImage replaces docker.NoSuchImage, which does not contain the image
20
// name, which in turn breaks docker clients post 1.7.0 since they expect the
21
// image name to be present in errors.
22
type ErrNoSuchImage struct {
23
        Name string
24
}
25

26
func (err *ErrNoSuchImage) Error() string {
!
27
        return "No such image: " + err.Name
!
28
}
!
29

30
func (i *createContainerInterceptor) InterceptRequest(r *http.Request) error {
149×
31
        container := jsonObject{}
149×
32
        if err := unmarshalRequestBody(r, &container); err != nil {
149×
33
                return err
!
34
        }
!
35

36
        hostConfig, err := container.Object("HostConfig")
149×
37
        if err != nil {
149×
38
                return err
!
39
        }
!
40

41
        networkMode, err := hostConfig.String("NetworkMode")
149×
42
        if err != nil {
149×
43
                return err
!
44
        }
!
45

46
        env, err := container.StringArray("Env")
149×
47
        if err != nil {
149×
48
                return err
!
49
        }
!
50

51
        if cidrs, err := i.proxy.weaveCIDRs(networkMode, env); err != nil {
250×
52
                Log.Infof("Leaving container alone because %s", err)
101×
53
        } else {
149×
54
                Log.Infof("Creating container with WEAVE_CIDR \"%s\"", strings.Join(cidrs, " "))
48×
55
                if i.proxy.NoMulticastRoute {
48×
56
                        if err := addVolume(hostConfig, i.proxy.weaveWaitNomcastVolume, "/w", "ro"); err != nil {
!
57
                                return err
!
58
                        }
!
59
                } else {
48×
60
                        if err := addVolume(hostConfig, i.proxy.weaveWaitVolume, "/w", "ro"); err != nil {
48×
61
                                return err
!
62
                        }
!
63
                }
64
                if err := i.setWeaveWaitEntrypoint(container); err != nil {
48×
UNCOV
65
                        return err
!
UNCOV
66
                }
!
67
                hostname, err := i.containerHostname(r, container)
48×
68
                if err != nil {
48×
69
                        return err
!
70
                }
!
71
                if dnsDomain := i.proxy.getDNSDomain(); dnsDomain != "" {
89×
72
                        if err := i.setHostname(container, hostname, dnsDomain); err != nil {
41×
73
                                return err
!
74
                        }
!
75
                        if err := i.proxy.setWeaveDNS(hostConfig, hostname, dnsDomain); err != nil {
41×
76
                                return err
!
77
                        }
!
78
                }
79

80
                return marshalRequestBody(r, container)
48×
81
        }
82

83
        return nil
101×
84
}
85

86
func (i *createContainerInterceptor) setWeaveWaitEntrypoint(container jsonObject) error {
48×
87
        var entrypoint []string
48×
88
        entrypoint, err := container.StringArray("Entrypoint")
48×
89
        if err != nil {
48×
90
                return err
!
91
        }
!
92

93
        cmd, err := container.StringArray("Cmd")
48×
94
        if err != nil {
48×
95
                return err
!
96
        }
!
97

98
        if len(entrypoint) == 0 {
96×
99
                containerImage, err := container.String("Image")
48×
100
                if err != nil {
48×
101
                        return err
!
102
                }
!
103

104
                image, err := i.proxy.client.InspectImage(containerImage)
48×
105
                if err == docker.ErrNoSuchImage {
48×
106
                        return &ErrNoSuchImage{containerImage}
!
107
                } else if err != nil {
48×
108
                        return err
!
109
                }
!
110

111
                if len(cmd) == 0 {
50×
112
                        cmd = image.Config.Cmd
2×
113
                        container["Cmd"] = cmd
2×
114
                }
2×
115

116
                if entrypoint == nil {
96×
117
                        entrypoint = image.Config.Entrypoint
48×
118
                        container["Entrypoint"] = entrypoint
48×
119
                }
48×
120
        }
121

122
        if len(entrypoint) == 0 && len(cmd) == 0 {
48×
UNCOV
123
                return ErrNoCommandSpecified
!
UNCOV
124
        }
!
125

126
        if len(entrypoint) == 0 || entrypoint[0] != weaveWaitEntrypoint[0] {
96×
127
                container["Entrypoint"] = append(weaveWaitEntrypoint, entrypoint...)
48×
128
        }
48×
129

130
        return nil
48×
131
}
132

133
func (i *createContainerInterceptor) setHostname(container jsonObject, name, dnsDomain string) error {
41×
134
        hostname, err := container.String("Hostname")
41×
135
        if err != nil {
41×
136
                return err
!
137
        }
!
138
        if hostname == "" && name != "" {
76×
139
                // Strip trailing period because it's unusual to see it used on the end of a host name
35×
140
                trimmedDNSDomain := strings.TrimSuffix(dnsDomain, ".")
35×
141
                if len(name)+1+len(trimmedDNSDomain) > MaxDockerHostname {
35×
142
                        Log.Warningf("Container name [%s] too long to be used as hostname", name)
!
143
                } else {
35×
144
                        container["Hostname"] = name
35×
145
                        container["Domainname"] = trimmedDNSDomain
35×
146
                }
35×
147
        }
148

149
        return nil
41×
150
}
151

152
func (i *createContainerInterceptor) InterceptResponse(r *http.Response) error {
149×
153
        return nil
149×
154
}
149×
155

156
func (i *createContainerInterceptor) containerHostname(r *http.Request, container jsonObject) (hostname string, err error) {
48×
157
        hostname = r.URL.Query().Get("name")
48×
158
        if i.proxy.Config.HostnameFromLabel != "" {
60×
159
                hostname, err = i.hostnameFromLabel(hostname, container)
12×
160
        }
12×
161
        hostname = i.proxy.hostnameMatchRegexp.ReplaceAllString(hostname, i.proxy.HostnameReplacement)
48×
162
        return
48×
163
}
164

165
func (i *createContainerInterceptor) hostnameFromLabel(hostname string, container jsonObject) (string, error) {
12×
166
        labels, err := container.Object("Labels")
12×
167
        if err != nil {
12×
168
                return "", err
!
169
        }
!
170
        label, err := labels.String(i.proxy.Config.HostnameFromLabel)
12×
171
        if err != nil {
12×
172
                return "", err
!
173
        }
!
174
        if label == "" {
18×
175
                return hostname, nil
6×
176
        }
6×
177

178
        return label, nil
6×
179
}
Troubleshooting · Open an Issue · Sales · Support · ENTERPRISE · CAREERS · STATUS
BLOG · TWITTER · Legal & Privacy · Supported CI Services · What's a CI service? · Automated Testing

© 2022 Coveralls, Inc