-
Notifications
You must be signed in to change notification settings - Fork 1.8k
*: test the case when both app and CA TLS assets exist #439
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,103 @@ | ||
// Copyright 2018 The Operator-SDK 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 e2e | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/operator-framework/operator-sdk/pkg/tlsutil" | ||
framework "github.com/operator-framework/operator-sdk/test/e2e/framework" | ||
|
||
"k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
// TestBothAppAndCATLSAssetsExist ensures that when both application | ||
// and CA TLS assets exist in the k8s cluster for a given cr, | ||
// the GenerateCert() simply returns those to the caller. | ||
func TestBothAppAndCATLSAssetsExist(t *testing.T) { | ||
f := framework.Global | ||
ctx := f.NewTestCtx(t) | ||
defer ctx.Cleanup(t) | ||
namespace, err := ctx.GetNamespace() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// Use Pod as a dummy runtime object for the CR input of GenerateCert(). | ||
crKind := "Pod" | ||
crName := "example-pod" | ||
mCR := &v1.Pod{ | ||
TypeMeta: metav1.TypeMeta{ | ||
Kind: crKind, | ||
}, | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: crName, | ||
Namespace: namespace, | ||
}, | ||
} | ||
|
||
certName := "app-cert" | ||
appSecret := &v1.Secret{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: tlsutil.ToAppSecretName(crKind, crName, certName), | ||
}, | ||
} | ||
appSecret, err = f.KubeClient.CoreV1().Secrets(namespace).Create(appSecret) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
caConfigMapAndSecretName := tlsutil.ToCASecretAndConfigMapName(crKind, crName) | ||
caConfigMap := &v1.ConfigMap{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: caConfigMapAndSecretName, | ||
}, | ||
} | ||
caConfigMap, err = f.KubeClient.CoreV1().ConfigMaps(namespace).Create(caConfigMap) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
caSecret := &v1.Secret{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: caConfigMapAndSecretName, | ||
}, | ||
} | ||
caSecret, err = f.KubeClient.CoreV1().Secrets(namespace).Create(caSecret) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
cg := tlsutil.NewSDKCertGenerator(f.KubeClient) | ||
ccfg := &tlsutil.CertConfig{ | ||
CertName: certName, | ||
} | ||
actualAppSecret, actualCaConfigMap, actualCaSecret, err := cg.GenerateCert(mCR, nil, ccfg) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if !reflect.DeepEqual(appSecret, actualAppSecret) { | ||
t.Fatalf("expect %+v, got %+v", appSecret, actualAppSecret) | ||
} | ||
if !reflect.DeepEqual(caConfigMap, actualCaConfigMap) { | ||
t.Fatalf("expect %+v, got %+v", caConfigMap, actualCaConfigMap) | ||
} | ||
if !reflect.DeepEqual(caSecret, actualCaSecret) { | ||
t.Fatalf("expect %+v, got %+v", caSecret, actualCaSecret) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we avoid doing DeepEqual() for the entire objects. That's usually not very helpful as things like resourceVersion might change if the object is updated after creation. Or some label or annotation might be added by the server after creation.
The checks should be as specific as possible. Either check something in the spec to verify equality. Or do it for the UID which uniquely identifies the object to verify they're both the same objects.
or
since they're just strings.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's usually not very helpful as things like resourceVersion might change if the object is updated after creation
This shouldn't happen because we don't update the secrets and configmap in this test case. I expect the created objects are the exact same as the one returned by GenerateCert().
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's okay for this case but my general point is when either reconciling or checking a test condition we should be as specific as possible when checking the equality of two objects.
In this case even if say the resourceVersion changes (for whatever reason) our test should not fail because we just care if it's the same secret object that we created earlier. And that's verifiable by just looking at something unique(like the UID).
Although even the name/namespace uniquely identifies the secret so we don't even need this check strictly speaking.
But you can keep it as
!reflect.DeepEqual(appSecret.UID, actualAppSecret.UID)
for this case.