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

weaveworks / weave / #3760

22 Sep 2015 - 13:52 coverage increased (+0.3%) to 77.586%
#3760

Pull #1449

circleci

Eac6f57e46837c7b0d0bacac4288d0b9?size=18&default=identiconpaulbellamy
Removing confusing variable from getDNSDomain, just return "" when disabled
Pull Request #1449: Override sneaky /start HostConfig params

44 of 70 new or added lines in 3 files covered. (62.86%)

3 existing lines in 1 file now uncovered.

5317 of 6853 relevant lines covered (77.59%)

126843.01 hits per line

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

74.29
/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
        . "github.com/weaveworks/weave/common"
10
)
11

12
const MaxDockerHostname = 64
13

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

18
type createContainerInterceptor struct{ proxy *Proxy }
19

20
type createContainerRequestBody struct {
21
        *docker.Config
22
        HostConfig *docker.HostConfig `json:"HostConfig,omitempty" yaml:"HostConfig,omitempty"`
23
        MacAddress string             `json:"MacAddress,omitempty" yaml:"MacAddress,omitempty"`
24
}
25

26
// ErrNoSuchImage replaces docker.NoSuchImage, which does not contain the image
27
// name, which in turn breaks docker clients post 1.7.0 since they expect the
28
// image name to be present in errors.
29
type ErrNoSuchImage struct {
30
        Name string
31
}
32

33
func (err *ErrNoSuchImage) Error() string {
1×
34
        return "No such image: " + err.Name
1×
35
}
1×
36

37
func (i *createContainerInterceptor) InterceptRequest(r *http.Request) error {
104×
38
        container := createContainerRequestBody{}
104×
39
        if err := unmarshalRequestBody(r, &container); err != nil {
!
40
                return err
!
41
        }
!
42

43
        if cidrs, err := i.proxy.weaveCIDRsFromConfig(container.Config, container.HostConfig); err != nil {
72×
44
                Log.Infof("Leaving container alone because %s", err)
72×
45
        } else {
32×
46
                Log.Infof("Creating container with WEAVE_CIDR \"%s\"", strings.Join(cidrs, " "))
32×
47
                if container.HostConfig == nil {
1×
48
                        container.HostConfig = &docker.HostConfig{}
1×
49
                }
1×
50
                if container.Config == nil {
!
51
                        container.Config = &docker.Config{}
!
52
                }
!
53
                i.proxy.addWeaveWaitVolume(container.HostConfig)
32×
54
                if err := i.setWeaveWaitEntrypoint(container.Config); err != nil {
2×
55
                        return err
2×
56
                }
2×
57
                hostname := r.URL.Query().Get("name")
30×
58
                if i.proxy.Config.HostnameFromLabel != "" {
!
59
                        if labelValue, ok := container.Labels[i.proxy.Config.HostnameFromLabel]; ok {
!
60
                                hostname = labelValue
!
61
                        }
!
62
                }
63
                hostname = i.proxy.hostnameMatchRegexp.ReplaceAllString(hostname, i.proxy.HostnameReplacement)
30×
64
                if dnsDomain := i.proxy.getDNSDomain(); dnsDomain != "" {
20×
65
                        i.setHostname(&container, hostname, dnsDomain)
20×
NEW
66
                        if err := i.proxy.setWeaveDNS(container.HostConfig, container.Hostname, dnsDomain); err != nil {
!
NEW
67
                                return err
!
NEW
68
                        }
!
69
                }
70

71
                return marshalRequestBody(r, container)
30×
72
        }
73

74
        return nil
72×
75
}
76

77
func (i *createContainerInterceptor) setWeaveWaitEntrypoint(container *docker.Config) error {
32×
78
        if len(container.Entrypoint) == 0 {
32×
79
                image, err := i.proxy.client.InspectImage(container.Image)
32×
80
                if err == docker.ErrNoSuchImage {
1×
81
                        return &ErrNoSuchImage{container.Image}
1×
82
                } else if err != nil {
!
83
                        return err
!
84
                }
!
85

86
                if len(container.Cmd) == 0 {
4×
87
                        container.Cmd = image.Config.Cmd
4×
88
                }
4×
89

90
                if container.Entrypoint == nil {
31×
91
                        container.Entrypoint = image.Config.Entrypoint
31×
92
                }
31×
93
        }
94

95
        if len(container.Entrypoint) == 0 && len(container.Cmd) == 0 {
1×
96
                return ErrNoCommandSpecified
1×
97
        }
1×
98

99
        if len(container.Entrypoint) == 0 || container.Entrypoint[0] != weaveWaitEntrypoint[0] {
29×
100
                container.Entrypoint = append(weaveWaitEntrypoint, container.Entrypoint...)
29×
101
        }
29×
102

103
        return nil
30×
104
}
105

106
func (i *createContainerInterceptor) setHostname(container *createContainerRequestBody, name, dnsDomain string) {
20×
107
        if container.Hostname == "" && name != "" {
15×
108
                // Strip trailing period because it's unusual to see it used on the end of a host name
15×
109
                trimmedDNSDomain := strings.TrimSuffix(dnsDomain, ".")
15×
110
                if len(name)+1+len(trimmedDNSDomain) > MaxDockerHostname {
!
111
                        Log.Warningf("Container name [%s] too long to be used as hostname", name)
!
112
                } else {
15×
113
                        container.Hostname = name
15×
114
                        container.Domainname = trimmedDNSDomain
15×
115
                }
15×
116
        }
117

118
        return
20×
119
}
120

121
func (i *createContainerInterceptor) InterceptResponse(r *http.Response) error {
102×
122
        return nil
102×
123
}
102×
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