mirror of
				https://github.com/optim-enterprises-bv/kubernetes.git
				synced 2025-11-03 19:58:17 +00:00 
			
		
		
		
	Move extensions.LabelSelector to unversioned.
Move type LabelSelector and type LabelSelectorRequirement from pkg/apis/extensions This avoids an import loop when Job (and later DaemonSet, Deployment, ReplicaSet) are moved out of extensions to new api groups. Also Move LabelSelectorAsSelector utility from pkg/apis/extensions/ to pkg/api/unversioned/ Also its test. Also LabelSelectorOp* constants. Also the pkg/apis/extensions/validation functions ValidateLabelSelectorRequirement and ValidateLabelSelector move to pkg/api/unversioned The related type in pkg/apis/extensions/v1beta1/ is staying there. I might move it in another PR if neccessary.
This commit is contained in:
		
							
								
								
									
										83
									
								
								pkg/api/unversioned/helpers_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										83
									
								
								pkg/api/unversioned/helpers_test.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,83 @@
 | 
			
		||||
/*
 | 
			
		||||
Copyright 2016 The Kubernetes Authors All rights reserved.
 | 
			
		||||
 | 
			
		||||
Licensed under the Apache License, Version 2.0 (the "License");
 | 
			
		||||
you may not use this file except in compliance with the License.
 | 
			
		||||
You may obtain a copy of the License at
 | 
			
		||||
 | 
			
		||||
    http://www.apache.org/licenses/LICENSE-2.0
 | 
			
		||||
 | 
			
		||||
Unless required by applicable law or agreed to in writing, software
 | 
			
		||||
distributed under the License is distributed on an "AS IS" BASIS,
 | 
			
		||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 | 
			
		||||
See the License for the specific language governing permissions and
 | 
			
		||||
limitations under the License.
 | 
			
		||||
*/
 | 
			
		||||
 | 
			
		||||
package unversioned
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"reflect"
 | 
			
		||||
	"testing"
 | 
			
		||||
 | 
			
		||||
	"k8s.io/kubernetes/pkg/labels"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func TestLabelSelectorAsSelector(t *testing.T) {
 | 
			
		||||
	matchLabels := map[string]string{"foo": "bar"}
 | 
			
		||||
	matchExpressions := []LabelSelectorRequirement{{
 | 
			
		||||
		Key:      "baz",
 | 
			
		||||
		Operator: LabelSelectorOpIn,
 | 
			
		||||
		Values:   []string{"qux", "norf"},
 | 
			
		||||
	}}
 | 
			
		||||
	mustParse := func(s string) labels.Selector {
 | 
			
		||||
		out, e := labels.Parse(s)
 | 
			
		||||
		if e != nil {
 | 
			
		||||
			panic(e)
 | 
			
		||||
		}
 | 
			
		||||
		return out
 | 
			
		||||
	}
 | 
			
		||||
	tc := []struct {
 | 
			
		||||
		in        *LabelSelector
 | 
			
		||||
		out       labels.Selector
 | 
			
		||||
		expectErr bool
 | 
			
		||||
	}{
 | 
			
		||||
		{in: nil, out: labels.Nothing()},
 | 
			
		||||
		{in: &LabelSelector{}, out: labels.Everything()},
 | 
			
		||||
		{
 | 
			
		||||
			in:  &LabelSelector{MatchLabels: matchLabels},
 | 
			
		||||
			out: mustParse("foo in (bar)"),
 | 
			
		||||
		},
 | 
			
		||||
		{
 | 
			
		||||
			in:  &LabelSelector{MatchExpressions: matchExpressions},
 | 
			
		||||
			out: mustParse("baz in (norf,qux)"),
 | 
			
		||||
		},
 | 
			
		||||
		{
 | 
			
		||||
			in:  &LabelSelector{MatchLabels: matchLabels, MatchExpressions: matchExpressions},
 | 
			
		||||
			out: mustParse("foo in (bar),baz in (norf,qux)"),
 | 
			
		||||
		},
 | 
			
		||||
		{
 | 
			
		||||
			in: &LabelSelector{
 | 
			
		||||
				MatchExpressions: []LabelSelectorRequirement{{
 | 
			
		||||
					Key:      "baz",
 | 
			
		||||
					Operator: LabelSelectorOpExists,
 | 
			
		||||
					Values:   []string{"qux", "norf"},
 | 
			
		||||
				}},
 | 
			
		||||
			},
 | 
			
		||||
			expectErr: true,
 | 
			
		||||
		},
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	for i, tc := range tc {
 | 
			
		||||
		out, err := LabelSelectorAsSelector(tc.in)
 | 
			
		||||
		if err == nil && tc.expectErr {
 | 
			
		||||
			t.Errorf("[%v]expected error but got none.", i)
 | 
			
		||||
		}
 | 
			
		||||
		if err != nil && !tc.expectErr {
 | 
			
		||||
			t.Errorf("[%v]did not expect error but got: %v", i, err)
 | 
			
		||||
		}
 | 
			
		||||
		if !reflect.DeepEqual(out, tc.out) {
 | 
			
		||||
			t.Errorf("[%v]expected:\n\t%+v\nbut got:\n\t%+v", i, tc.out, out)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user