-
Notifications
You must be signed in to change notification settings - Fork 303
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make annotation wrapper types internal keys private; adds unit tests
This prevents uses of raw annotation keys without proper accessor methods.
- Loading branch information
Showing
7 changed files
with
218 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
Copyright 2017 The Kubernetes Authors. | ||
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 annotations | ||
|
||
import ( | ||
"testing" | ||
|
||
extensions "k8s.io/api/extensions/v1beta1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
func TestIngress(t *testing.T) { | ||
for _, tc := range []struct { | ||
ing *extensions.Ingress | ||
allowHTTP bool | ||
useNamedTLS string | ||
staticIPName string | ||
ingressClass string | ||
}{ | ||
{ | ||
ing: &extensions.Ingress{}, | ||
allowHTTP: true, // defaults to true. | ||
}, | ||
{ | ||
ing: &extensions.Ingress{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Annotations: map[string]string{ | ||
AllowHTTPKey: "false", | ||
IngressClassKey: "gce", | ||
PreSharedCertKey: "shared-cert-key", | ||
StaticIPNameKey: "1.2.3.4", | ||
}, | ||
}, | ||
}, | ||
allowHTTP: false, | ||
useNamedTLS: "shared-cert-key", | ||
staticIPName: "1.2.3.4", | ||
ingressClass: "gce", | ||
}, | ||
} { | ||
ing := FromIngress(tc.ing) | ||
if x := ing.AllowHTTP(); x != tc.allowHTTP { | ||
t.Errorf("ingress %+v; AllowHTTP() = %v, want %v", tc.ing, x, tc.allowHTTP) | ||
} | ||
if x := ing.UseNamedTLS(); x != tc.useNamedTLS { | ||
t.Errorf("ingress %+v; UseNamedTLS() = %v, want %v", tc.ing, x, tc.useNamedTLS) | ||
} | ||
if x := ing.StaticIPName(); x != tc.staticIPName { | ||
t.Errorf("ingress %+v; StaticIPName() = %v, want %v", tc.ing, x, tc.staticIPName) | ||
} | ||
if x := ing.IngressClass(); x != tc.ingressClass { | ||
t.Errorf("ingress %+v; IngressClass() = %v, want %v", tc.ing, x, tc.ingressClass) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* | ||
Copyright 2017 The Kubernetes Authors. | ||
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 annotations | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
func TestService(t *testing.T) { | ||
for _, tc := range []struct { | ||
svc *v1.Service | ||
appProtocolsErr bool | ||
appProtocols map[string]AppProtocol | ||
neg bool | ||
}{ | ||
{ | ||
svc: &v1.Service{}, | ||
appProtocols: map[string]AppProtocol{}, | ||
}, | ||
{ | ||
svc: &v1.Service{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Annotations: map[string]string{ | ||
ServiceApplicationProtocolKey: `{"80": "HTTP", "443": "HTTPS"}`, | ||
}, | ||
}, | ||
}, | ||
appProtocols: map[string]AppProtocol{"80": "HTTP", "443": "HTTPS"}, | ||
}, | ||
{ | ||
svc: &v1.Service{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Annotations: map[string]string{ | ||
NetworkEndpointGroupAlphaAnnotation: "true", | ||
}, | ||
}, | ||
}, | ||
appProtocols: map[string]AppProtocol{}, | ||
neg: true, | ||
}, | ||
{ | ||
svc: &v1.Service{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Annotations: map[string]string{ | ||
ServiceApplicationProtocolKey: `invalid`, | ||
}, | ||
}, | ||
}, | ||
appProtocolsErr: true, | ||
}, | ||
{ | ||
svc: &v1.Service{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Annotations: map[string]string{ | ||
ServiceApplicationProtocolKey: `{"SSH": "22"}`, | ||
}, | ||
}, | ||
}, | ||
appProtocolsErr: true, | ||
}, | ||
} { | ||
svc := FromService(tc.svc) | ||
ap, err := svc.ApplicationProtocols() | ||
if tc.appProtocolsErr { | ||
if err == nil { | ||
t.Errorf("for service %+v; svc.ApplicationProtocols() = _, %v; want _, error", tc.svc, err) | ||
} | ||
continue | ||
} | ||
if err != nil || !reflect.DeepEqual(ap, tc.appProtocols) { | ||
t.Errorf("for service %+v; svc.ApplicationProtocols() = %v, %v; want %v, nil", tc.svc, ap, err, tc.appProtocols) | ||
} | ||
if b := svc.NEGEnabled(); b != tc.neg { | ||
t.Errorf("for service %+v; svc.NEGEnabled() = %v; want %v", tc.svc, b, tc.neg) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters