Browse code

Tests for namespaced pruning and necessary refactor

Maciej Szulik authored on 2016/10/07 22:22:43
Showing 6 changed files
... ...
@@ -41,10 +41,10 @@ type PruneBuildsOptions struct {
41 41
 	KeepYoungerThan time.Duration
42 42
 	KeepComplete    int
43 43
 	KeepFailed      int
44
+	Namespace       string
44 45
 
45
-	Pruner prune.Pruner
46
-	Client client.Interface
47
-	Out    io.Writer
46
+	OSClient client.Interface
47
+	Out      io.Writer
48 48
 }
49 49
 
50 50
 // NewCmdPruneBuilds implements the OpenShift cli prune builds command.
... ...
@@ -84,10 +84,10 @@ func (o *PruneBuildsOptions) Complete(f *clientcmd.Factory, cmd *cobra.Command,
84 84
 		return kcmdutil.UsageError(cmd, "no arguments are allowed to this command")
85 85
 	}
86 86
 
87
-	namespace := kapi.NamespaceAll
87
+	o.Namespace = kapi.NamespaceAll
88 88
 	if cmd.Flags().Lookup("namespace").Changed {
89 89
 		var err error
90
-		namespace, _, err = f.DefaultNamespace()
90
+		o.Namespace, _, err = f.DefaultNamespace()
91 91
 		if err != nil {
92 92
 			return err
93 93
 		}
... ...
@@ -98,9 +98,28 @@ func (o *PruneBuildsOptions) Complete(f *clientcmd.Factory, cmd *cobra.Command,
98 98
 	if err != nil {
99 99
 		return err
100 100
 	}
101
-	o.Client = osClient
101
+	o.OSClient = osClient
102 102
 
103
-	buildConfigList, err := osClient.BuildConfigs(namespace).List(kapi.ListOptions{})
103
+	return nil
104
+}
105
+
106
+// Validate ensures that a PruneBuildsOptions is valid and can be used to execute pruning.
107
+func (o PruneBuildsOptions) Validate() error {
108
+	if o.KeepYoungerThan < 0 {
109
+		return fmt.Errorf("--keep-younger-than must be greater than or equal to 0")
110
+	}
111
+	if o.KeepComplete < 0 {
112
+		return fmt.Errorf("--keep-complete must be greater than or equal to 0")
113
+	}
114
+	if o.KeepFailed < 0 {
115
+		return fmt.Errorf("--keep-failed must be greater than or equal to 0")
116
+	}
117
+	return nil
118
+}
119
+
120
+// Run contains all the necessary functionality for the OpenShift cli prune builds command.
121
+func (o PruneBuildsOptions) Run() error {
122
+	buildConfigList, err := o.OSClient.BuildConfigs(o.Namespace).List(kapi.ListOptions{})
104 123
 	if err != nil {
105 124
 		return err
106 125
 	}
... ...
@@ -109,7 +128,7 @@ func (o *PruneBuildsOptions) Complete(f *clientcmd.Factory, cmd *cobra.Command,
109 109
 		buildConfigs = append(buildConfigs, &buildConfigList.Items[i])
110 110
 	}
111 111
 
112
-	buildList, err := osClient.Builds(namespace).List(kapi.ListOptions{})
112
+	buildList, err := o.OSClient.Builds(o.Namespace).List(kapi.ListOptions{})
113 113
 	if err != nil {
114 114
 		return err
115 115
 	}
... ...
@@ -126,40 +145,20 @@ func (o *PruneBuildsOptions) Complete(f *clientcmd.Factory, cmd *cobra.Command,
126 126
 		BuildConfigs:    buildConfigs,
127 127
 		Builds:          builds,
128 128
 	}
129
+	pruner := prune.NewPruner(options)
129 130
 
130
-	o.Pruner = prune.NewPruner(options)
131
-
132
-	return nil
133
-}
134
-
135
-// Validate ensures that a PruneBuildsOptions is valid and can be used to execute pruning.
136
-func (o PruneBuildsOptions) Validate() error {
137
-	if o.KeepYoungerThan < 0 {
138
-		return fmt.Errorf("--keep-younger-than must be greater than or equal to 0")
139
-	}
140
-	if o.KeepComplete < 0 {
141
-		return fmt.Errorf("--keep-complete must be greater than or equal to 0")
142
-	}
143
-	if o.KeepFailed < 0 {
144
-		return fmt.Errorf("--keep-failed must be greater than or equal to 0")
145
-	}
146
-	return nil
147
-}
148
-
149
-// Run contains all the necessary functionality for the OpenShift cli prune builds command.
150
-func (o PruneBuildsOptions) Run() error {
151 131
 	w := tabwriter.NewWriter(o.Out, 10, 4, 3, ' ', 0)
152 132
 	defer w.Flush()
153 133
 
154 134
 	buildDeleter := &describingBuildDeleter{w: w}
155 135
 
156 136
 	if o.Confirm {
157
-		buildDeleter.delegate = prune.NewBuildDeleter(o.Client)
137
+		buildDeleter.delegate = prune.NewBuildDeleter(o.OSClient)
158 138
 	} else {
159 139
 		fmt.Fprintln(os.Stderr, "Dry run enabled - no modifications will be made. Add --confirm to remove builds")
160 140
 	}
161 141
 
162
-	return o.Pruner.Prune(buildDeleter)
142
+	return pruner.Prune(buildDeleter)
163 143
 }
164 144
 
165 145
 // describingBuildDeleter prints information about each build it removes.
166 146
new file mode 100644
... ...
@@ -0,0 +1,31 @@
0
+package prune
1
+
2
+import (
3
+	"io/ioutil"
4
+	"testing"
5
+
6
+	"github.com/openshift/origin/pkg/client/testclient"
7
+)
8
+
9
+func TestBuildPruneNamespaced(t *testing.T) {
10
+	osFake := testclient.NewSimpleFake()
11
+	opts := &PruneBuildsOptions{
12
+		Namespace: "foo",
13
+
14
+		OSClient: osFake,
15
+		Out:      ioutil.Discard,
16
+	}
17
+
18
+	if err := opts.Run(); err != nil {
19
+		t.Errorf("Unexpected error: %v", err)
20
+	}
21
+
22
+	if len(osFake.Actions()) == 0 {
23
+		t.Errorf("Missing get build actions")
24
+	}
25
+	for _, a := range osFake.Actions() {
26
+		if a.GetNamespace() != "foo" {
27
+			t.Errorf("Unexpected namespace while pruning %s: %s", a.GetResource(), a.GetNamespace())
28
+		}
29
+	}
30
+}
... ...
@@ -13,6 +13,7 @@ import (
13 13
 	kclient "k8s.io/kubernetes/pkg/client/unversioned"
14 14
 	kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
15 15
 
16
+	"github.com/openshift/origin/pkg/client"
16 17
 	"github.com/openshift/origin/pkg/cmd/util/clientcmd"
17 18
 	deployapi "github.com/openshift/origin/pkg/deploy/api"
18 19
 	"github.com/openshift/origin/pkg/deploy/prune"
... ...
@@ -41,10 +42,11 @@ type PruneDeploymentsOptions struct {
41 41
 	KeepYoungerThan time.Duration
42 42
 	KeepComplete    int
43 43
 	KeepFailed      int
44
+	Namespace       string
44 45
 
45
-	Pruner prune.Pruner
46
-	Client kclient.Interface
47
-	Out    io.Writer
46
+	OSClient client.Interface
47
+	KClient  kclient.Interface
48
+	Out      io.Writer
48 49
 }
49 50
 
50 51
 // NewCmdPruneDeployments implements the OpenShift cli prune deployments command.
... ...
@@ -85,10 +87,10 @@ func (o *PruneDeploymentsOptions) Complete(f *clientcmd.Factory, cmd *cobra.Comm
85 85
 		return kcmdutil.UsageError(cmd, "no arguments are allowed to this command")
86 86
 	}
87 87
 
88
-	namespace := kapi.NamespaceAll
88
+	o.Namespace = kapi.NamespaceAll
89 89
 	if cmd.Flags().Lookup("namespace").Changed {
90 90
 		var err error
91
-		namespace, _, err = f.DefaultNamespace()
91
+		o.Namespace, _, err = f.DefaultNamespace()
92 92
 		if err != nil {
93 93
 			return err
94 94
 		}
... ...
@@ -99,9 +101,29 @@ func (o *PruneDeploymentsOptions) Complete(f *clientcmd.Factory, cmd *cobra.Comm
99 99
 	if err != nil {
100 100
 		return err
101 101
 	}
102
-	o.Client = kClient
102
+	o.OSClient = osClient
103
+	o.KClient = kClient
103 104
 
104
-	deploymentConfigList, err := osClient.DeploymentConfigs(namespace).List(kapi.ListOptions{})
105
+	return nil
106
+}
107
+
108
+// Validate ensures that a PruneDeploymentsOptions is valid and can be used to execute pruning.
109
+func (o PruneDeploymentsOptions) Validate() error {
110
+	if o.KeepYoungerThan < 0 {
111
+		return fmt.Errorf("--keep-younger-than must be greater than or equal to 0")
112
+	}
113
+	if o.KeepComplete < 0 {
114
+		return fmt.Errorf("--keep-complete must be greater than or equal to 0")
115
+	}
116
+	if o.KeepFailed < 0 {
117
+		return fmt.Errorf("--keep-failed must be greater than or equal to 0")
118
+	}
119
+	return nil
120
+}
121
+
122
+// Run contains all the necessary functionality for the OpenShift cli prune deployments command.
123
+func (o PruneDeploymentsOptions) Run() error {
124
+	deploymentConfigList, err := o.OSClient.DeploymentConfigs(o.Namespace).List(kapi.ListOptions{})
105 125
 	if err != nil {
106 126
 		return err
107 127
 	}
... ...
@@ -110,7 +132,7 @@ func (o *PruneDeploymentsOptions) Complete(f *clientcmd.Factory, cmd *cobra.Comm
110 110
 		deploymentConfigs = append(deploymentConfigs, &deploymentConfigList.Items[i])
111 111
 	}
112 112
 
113
-	deploymentList, err := kClient.ReplicationControllers(namespace).List(kapi.ListOptions{})
113
+	deploymentList, err := o.KClient.ReplicationControllers(o.Namespace).List(kapi.ListOptions{})
114 114
 	if err != nil {
115 115
 		return err
116 116
 	}
... ...
@@ -127,40 +149,20 @@ func (o *PruneDeploymentsOptions) Complete(f *clientcmd.Factory, cmd *cobra.Comm
127 127
 		DeploymentConfigs: deploymentConfigs,
128 128
 		Deployments:       deployments,
129 129
 	}
130
+	pruner := prune.NewPruner(options)
130 131
 
131
-	o.Pruner = prune.NewPruner(options)
132
-
133
-	return nil
134
-}
135
-
136
-// Validate ensures that a PruneDeploymentsOptions is valid and can be used to execute pruning.
137
-func (o PruneDeploymentsOptions) Validate() error {
138
-	if o.KeepYoungerThan < 0 {
139
-		return fmt.Errorf("--keep-younger-than must be greater than or equal to 0")
140
-	}
141
-	if o.KeepComplete < 0 {
142
-		return fmt.Errorf("--keep-complete must be greater than or equal to 0")
143
-	}
144
-	if o.KeepFailed < 0 {
145
-		return fmt.Errorf("--keep-failed must be greater than or equal to 0")
146
-	}
147
-	return nil
148
-}
149
-
150
-// Run contains all the necessary functionality for the OpenShift cli prune deployments command.
151
-func (o PruneDeploymentsOptions) Run() error {
152 132
 	w := tabwriter.NewWriter(o.Out, 10, 4, 3, ' ', 0)
153 133
 	defer w.Flush()
154 134
 
155 135
 	deploymentDeleter := &describingDeploymentDeleter{w: w}
156 136
 
157 137
 	if o.Confirm {
158
-		deploymentDeleter.delegate = prune.NewDeploymentDeleter(o.Client, o.Client)
138
+		deploymentDeleter.delegate = prune.NewDeploymentDeleter(o.KClient, o.KClient)
159 139
 	} else {
160 140
 		fmt.Fprintln(os.Stderr, "Dry run enabled - no modifications will be made. Add --confirm to remove deployments")
161 141
 	}
162 142
 
163
-	return o.Pruner.Prune(deploymentDeleter)
143
+	return pruner.Prune(deploymentDeleter)
164 144
 }
165 145
 
166 146
 // describingDeploymentDeleter prints information about each deployment it removes.
167 147
new file mode 100644
... ...
@@ -0,0 +1,40 @@
0
+package prune
1
+
2
+import (
3
+	"io/ioutil"
4
+	"testing"
5
+
6
+	ktestclient "k8s.io/kubernetes/pkg/client/unversioned/testclient"
7
+
8
+	"github.com/openshift/origin/pkg/client/testclient"
9
+)
10
+
11
+func TestDeploymentPruneNamespaced(t *testing.T) {
12
+	kFake := ktestclient.NewSimpleFake()
13
+	osFake := testclient.NewSimpleFake()
14
+	opts := &PruneDeploymentsOptions{
15
+		Namespace: "foo",
16
+
17
+		OSClient: osFake,
18
+		KClient:  kFake,
19
+		Out:      ioutil.Discard,
20
+	}
21
+
22
+	if err := opts.Run(); err != nil {
23
+		t.Errorf("Unexpected error: %v", err)
24
+	}
25
+
26
+	if len(osFake.Actions()) == 0 || len(kFake.Actions()) == 0 {
27
+		t.Errorf("Missing get deployments actions")
28
+	}
29
+	for _, a := range osFake.Actions() {
30
+		if a.GetNamespace() != "foo" {
31
+			t.Errorf("Unexpected namespace while pruning %s: %s", a.GetResource(), a.GetNamespace())
32
+		}
33
+	}
34
+	for _, a := range kFake.Actions() {
35
+		if a.GetNamespace() != "foo" {
36
+			t.Errorf("Unexpected namespace while pruning %s: %s", a.GetResource(), a.GetNamespace())
37
+		}
38
+	}
39
+}
... ...
@@ -69,10 +69,12 @@ type PruneImagesOptions struct {
69 69
 	PruneOverSizeLimit  *bool
70 70
 	CABundle            string
71 71
 	RegistryUrlOverride string
72
+	Namespace           string
72 73
 
73
-	Pruner prune.Pruner
74
-	Client client.Interface
75
-	Out    io.Writer
74
+	OSClient       client.Interface
75
+	KClient        kclient.Interface
76
+	RegistryClient *http.Client
77
+	Out            io.Writer
76 78
 }
77 79
 
78 80
 // NewCmdPruneImages implements the OpenShift cli prune images command.
... ...
@@ -124,10 +126,10 @@ func (o *PruneImagesOptions) Complete(f *clientcmd.Factory, cmd *cobra.Command,
124 124
 	if !cmd.Flags().Lookup("prune-over-size-limit").Changed {
125 125
 		o.PruneOverSizeLimit = nil
126 126
 	}
127
-	namespace := kapi.NamespaceAll
127
+	o.Namespace = kapi.NamespaceAll
128 128
 	if cmd.Flags().Lookup("namespace").Changed {
129 129
 		var err error
130
-		namespace, _, err = f.DefaultNamespace()
130
+		o.Namespace, _, err = f.DefaultNamespace()
131 131
 		if err != nil {
132 132
 			return err
133 133
 		}
... ...
@@ -138,48 +140,72 @@ func (o *PruneImagesOptions) Complete(f *clientcmd.Factory, cmd *cobra.Command,
138 138
 	if err != nil {
139 139
 		return err
140 140
 	}
141
-	o.Client = osClient
141
+	o.OSClient = osClient
142
+	o.KClient = kClient
143
+	o.RegistryClient = registryClient
142 144
 
143
-	allImages, err := osClient.Images().List(kapi.ListOptions{})
145
+	return nil
146
+}
147
+
148
+// Validate ensures that a PruneImagesOptions is valid and can be used to execute pruning.
149
+func (o PruneImagesOptions) Validate() error {
150
+	if o.PruneOverSizeLimit != nil && (o.KeepYoungerThan != nil || o.KeepTagRevisions != nil) {
151
+		return fmt.Errorf("--prune-over-size-limit cannot be specified with --keep-tag-revisions nor --keep-younger-than")
152
+	}
153
+	if o.KeepYoungerThan != nil && *o.KeepYoungerThan < 0 {
154
+		return fmt.Errorf("--keep-younger-than must be greater than or equal to 0")
155
+	}
156
+	if o.KeepTagRevisions != nil && *o.KeepTagRevisions < 0 {
157
+		return fmt.Errorf("--keep-tag-revisions must be greater than or equal to 0")
158
+	}
159
+	if _, err := url.Parse(o.RegistryUrlOverride); err != nil {
160
+		return fmt.Errorf("invalid --registry-url flag: %v", err)
161
+	}
162
+	return nil
163
+}
164
+
165
+// Run contains all the necessary functionality for the OpenShift cli prune images command.
166
+func (o PruneImagesOptions) Run() error {
167
+	allImages, err := o.OSClient.Images().List(kapi.ListOptions{})
144 168
 	if err != nil {
145 169
 		return err
146 170
 	}
147 171
 
148
-	allStreams, err := osClient.ImageStreams(namespace).List(kapi.ListOptions{})
172
+	allStreams, err := o.OSClient.ImageStreams(o.Namespace).List(kapi.ListOptions{})
149 173
 	if err != nil {
150 174
 		return err
151 175
 	}
152 176
 
153
-	allPods, err := kClient.Pods(namespace).List(kapi.ListOptions{})
177
+	allPods, err := o.KClient.Pods(o.Namespace).List(kapi.ListOptions{})
154 178
 	if err != nil {
155 179
 		return err
156 180
 	}
157 181
 
158
-	allRCs, err := kClient.ReplicationControllers(namespace).List(kapi.ListOptions{})
182
+	allRCs, err := o.KClient.ReplicationControllers(o.Namespace).List(kapi.ListOptions{})
159 183
 	if err != nil {
160 184
 		return err
161 185
 	}
162 186
 
163
-	allBCs, err := osClient.BuildConfigs(namespace).List(kapi.ListOptions{})
187
+	allBCs, err := o.OSClient.BuildConfigs(o.Namespace).List(kapi.ListOptions{})
164 188
 	// We need to tolerate 'not found' errors for buildConfigs since they may be disabled in Atomic
165 189
 	err = oserrors.TolerateNotFoundError(err)
166 190
 	if err != nil {
167 191
 		return err
168 192
 	}
169 193
 
170
-	allBuilds, err := osClient.Builds(namespace).List(kapi.ListOptions{})
194
+	allBuilds, err := o.OSClient.Builds(o.Namespace).List(kapi.ListOptions{})
171 195
 	// We need to tolerate 'not found' errors for builds since they may be disabled in Atomic
172 196
 	err = oserrors.TolerateNotFoundError(err)
173 197
 	if err != nil {
174 198
 		return err
175 199
 	}
176 200
 
177
-	allDCs, err := osClient.DeploymentConfigs(namespace).List(kapi.ListOptions{})
201
+	allDCs, err := o.OSClient.DeploymentConfigs(o.Namespace).List(kapi.ListOptions{})
178 202
 	if err != nil {
179 203
 		return err
180 204
 	}
181 205
 
182
-	limitRangesList, err := kClient.LimitRanges(namespace).List(kapi.ListOptions{})
206
+	limitRangesList, err := o.KClient.LimitRanges(o.Namespace).List(kapi.ListOptions{})
183 207
 	if err != nil {
184 208
 		return err
185 209
 	}
... ...
@@ -207,37 +233,14 @@ func (o *PruneImagesOptions) Complete(f *clientcmd.Factory, cmd *cobra.Command,
207 207
 		DCs:                allDCs,
208 208
 		LimitRanges:        limitRangesMap,
209 209
 		DryRun:             o.Confirm == false,
210
-		RegistryClient:     registryClient,
210
+		RegistryClient:     o.RegistryClient,
211 211
 		RegistryURL:        o.RegistryUrlOverride,
212 212
 	}
213
-	if namespace != kapi.NamespaceAll {
214
-		options.Namespace = namespace
213
+	if o.Namespace != kapi.NamespaceAll {
214
+		options.Namespace = o.Namespace
215 215
 	}
216
+	pruner := prune.NewPruner(options)
216 217
 
217
-	o.Pruner = prune.NewPruner(options)
218
-
219
-	return nil
220
-}
221
-
222
-// Validate ensures that a PruneImagesOptions is valid and can be used to execute pruning.
223
-func (o PruneImagesOptions) Validate() error {
224
-	if o.PruneOverSizeLimit != nil && (o.KeepYoungerThan != nil || o.KeepTagRevisions != nil) {
225
-		return fmt.Errorf("--prune-over-size-limit cannot be specified with --keep-tag-revisions nor --keep-younger-than")
226
-	}
227
-	if o.KeepYoungerThan != nil && *o.KeepYoungerThan < 0 {
228
-		return fmt.Errorf("--keep-younger-than must be greater than or equal to 0")
229
-	}
230
-	if o.KeepTagRevisions != nil && *o.KeepTagRevisions < 0 {
231
-		return fmt.Errorf("--keep-tag-revisions must be greater than or equal to 0")
232
-	}
233
-	if _, err := url.Parse(o.RegistryUrlOverride); err != nil {
234
-		return fmt.Errorf("invalid --registry-url flag: %v", err)
235
-	}
236
-	return nil
237
-}
238
-
239
-// Run contains all the necessary functionality for the OpenShift cli prune images command.
240
-func (o PruneImagesOptions) Run() error {
241 218
 	w := tabwriter.NewWriter(o.Out, 10, 4, 3, ' ', 0)
242 219
 	defer w.Flush()
243 220
 
... ...
@@ -248,8 +251,8 @@ func (o PruneImagesOptions) Run() error {
248 248
 	manifestDeleter := &describingManifestDeleter{w: w}
249 249
 
250 250
 	if o.Confirm {
251
-		imageDeleter.delegate = prune.NewImageDeleter(o.Client.Images())
252
-		imageStreamDeleter.delegate = prune.NewImageStreamDeleter(o.Client)
251
+		imageDeleter.delegate = prune.NewImageDeleter(o.OSClient.Images())
252
+		imageStreamDeleter.delegate = prune.NewImageStreamDeleter(o.OSClient)
253 253
 		layerLinkDeleter.delegate = prune.NewLayerLinkDeleter()
254 254
 		blobDeleter.delegate = prune.NewBlobDeleter()
255 255
 		manifestDeleter.delegate = prune.NewManifestDeleter()
... ...
@@ -257,7 +260,7 @@ func (o PruneImagesOptions) Run() error {
257 257
 		fmt.Fprintln(os.Stderr, "Dry run enabled - no modifications will be made. Add --confirm to remove images")
258 258
 	}
259 259
 
260
-	return o.Pruner.Prune(imageDeleter, imageStreamDeleter, layerLinkDeleter, blobDeleter, manifestDeleter)
260
+	return pruner.Prune(imageDeleter, imageStreamDeleter, layerLinkDeleter, blobDeleter, manifestDeleter)
261 261
 }
262 262
 
263 263
 // describingImageStreamDeleter prints information about each image stream update.
264 264
new file mode 100644
... ...
@@ -0,0 +1,44 @@
0
+package prune
1
+
2
+import (
3
+	"io/ioutil"
4
+	"testing"
5
+
6
+	ktestclient "k8s.io/kubernetes/pkg/client/unversioned/testclient"
7
+
8
+	"github.com/openshift/origin/pkg/client/testclient"
9
+)
10
+
11
+func TestImagePruneNamespaced(t *testing.T) {
12
+	kFake := ktestclient.NewSimpleFake()
13
+	osFake := testclient.NewSimpleFake()
14
+	opts := &PruneImagesOptions{
15
+		Namespace: "foo",
16
+
17
+		OSClient: osFake,
18
+		KClient:  kFake,
19
+		Out:      ioutil.Discard,
20
+	}
21
+
22
+	if err := opts.Run(); err != nil {
23
+		t.Errorf("Unexpected error: %v", err)
24
+	}
25
+
26
+	if len(osFake.Actions()) == 0 || len(kFake.Actions()) == 0 {
27
+		t.Errorf("Missing get images actions")
28
+	}
29
+	for _, a := range osFake.Actions() {
30
+		// images are non-namespaced
31
+		if a.GetResource() == "images" {
32
+			continue
33
+		}
34
+		if a.GetNamespace() != "foo" {
35
+			t.Errorf("Unexpected namespace while pruning %s: %s", a.GetResource(), a.GetNamespace())
36
+		}
37
+	}
38
+	for _, a := range kFake.Actions() {
39
+		if a.GetNamespace() != "foo" {
40
+			t.Errorf("Unexpected namespace while pruning %s: %s", a.GetResource(), a.GetNamespace())
41
+		}
42
+	}
43
+}