Browse code

Remove unnecessary func parameter, add mirror endpoint test

Signed-off-by: Aidan Hobson Sayers <aidanhs@cantab.net>

Aidan Hobson Sayers authored on 2015/08/07 10:21:02
Showing 2 changed files
... ...
@@ -677,6 +677,35 @@ func TestNewIndexInfo(t *testing.T) {
677 677
 	testIndexInfo(config, expectedIndexInfos)
678 678
 }
679 679
 
680
+func TestMirrorEndpointLookup(t *testing.T) {
681
+	containsMirror := func(endpoints []APIEndpoint) bool {
682
+		for _, pe := range endpoints {
683
+			if pe.URL == "my.mirror" {
684
+				return true
685
+			}
686
+		}
687
+		return false
688
+	}
689
+	s := Service{Config: makeServiceConfig([]string{"my.mirror"}, nil)}
690
+	imageName := IndexName + "/test/image"
691
+
692
+	pushAPIEndpoints, err := s.LookupPushEndpoints(imageName)
693
+	if err != nil {
694
+		t.Fatal(err)
695
+	}
696
+	if containsMirror(pushAPIEndpoints) {
697
+		t.Fatal("Push endpoint should not contain mirror")
698
+	}
699
+
700
+	pullAPIEndpoints, err := s.LookupPullEndpoints(imageName)
701
+	if err != nil {
702
+		t.Fatal(err)
703
+	}
704
+	if !containsMirror(pullAPIEndpoints) {
705
+		t.Fatal("Pull endpoint should contain mirror")
706
+	}
707
+}
708
+
680 709
 func TestPushRegistryTag(t *testing.T) {
681 710
 	r := spawnTestRegistrySession(t)
682 711
 	err := r.PushRegistryTag("foo42/bar", imageID, "stable", makeURL("/v1/"))
... ...
@@ -113,36 +113,42 @@ func (s *Service) tlsConfigForMirror(mirror string) (*tls.Config, error) {
113 113
 // It gives preference to v2 endpoints over v1, mirrors over the actual
114 114
 // registry, and HTTPS over plain HTTP.
115 115
 func (s *Service) LookupPullEndpoints(repoName string) (endpoints []APIEndpoint, err error) {
116
-	return s.lookupEndpoints(repoName, false)
116
+	return s.lookupEndpoints(repoName)
117 117
 }
118 118
 
119 119
 // LookupPushEndpoints creates an list of endpoints to try to push to, in order of preference.
120 120
 // It gives preference to v2 endpoints over v1, and HTTPS over plain HTTP.
121 121
 // Mirrors are not included.
122 122
 func (s *Service) LookupPushEndpoints(repoName string) (endpoints []APIEndpoint, err error) {
123
-	return s.lookupEndpoints(repoName, true)
123
+	allEndpoints, err := s.lookupEndpoints(repoName)
124
+	if err == nil {
125
+		for _, endpoint := range allEndpoints {
126
+			if !endpoint.Mirror {
127
+				endpoints = append(endpoints, endpoint)
128
+			}
129
+		}
130
+	}
131
+	return endpoints, err
124 132
 }
125 133
 
126
-func (s *Service) lookupEndpoints(repoName string, isPush bool) (endpoints []APIEndpoint, err error) {
134
+func (s *Service) lookupEndpoints(repoName string) (endpoints []APIEndpoint, err error) {
127 135
 	var cfg = tlsconfig.ServerDefault
128 136
 	tlsConfig := &cfg
129 137
 	if strings.HasPrefix(repoName, DefaultNamespace+"/") {
130
-		if !isPush {
131
-			// v2 mirrors for pull only
132
-			for _, mirror := range s.Config.Mirrors {
133
-				mirrorTLSConfig, err := s.tlsConfigForMirror(mirror)
134
-				if err != nil {
135
-					return nil, err
136
-				}
137
-				endpoints = append(endpoints, APIEndpoint{
138
-					URL: mirror,
139
-					// guess mirrors are v2
140
-					Version:      APIVersion2,
141
-					Mirror:       true,
142
-					TrimHostname: true,
143
-					TLSConfig:    mirrorTLSConfig,
144
-				})
138
+		// v2 mirrors
139
+		for _, mirror := range s.Config.Mirrors {
140
+			mirrorTLSConfig, err := s.tlsConfigForMirror(mirror)
141
+			if err != nil {
142
+				return nil, err
145 143
 			}
144
+			endpoints = append(endpoints, APIEndpoint{
145
+				URL: mirror,
146
+				// guess mirrors are v2
147
+				Version:      APIVersion2,
148
+				Mirror:       true,
149
+				TrimHostname: true,
150
+				TLSConfig:    mirrorTLSConfig,
151
+			})
146 152
 		}
147 153
 		// v2 registry
148 154
 		endpoints = append(endpoints, APIEndpoint{