-
Notifications
You must be signed in to change notification settings - Fork 263
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added unit tests for completion helper
- Loading branch information
Showing
4 changed files
with
201 additions
and
0 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
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,197 @@ | ||
// Copyright © 2021 The Knative 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 commands | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"path" | ||
"testing" | ||
|
||
"github.com/spf13/cobra" | ||
"gotest.tools/v3/assert" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/cli-runtime/pkg/genericclioptions" | ||
clienttesting "k8s.io/client-go/testing" | ||
v1 "knative.dev/client/pkg/serving/v1" | ||
v12 "knative.dev/serving/pkg/apis/serving/v1" | ||
servingv1fake "knative.dev/serving/pkg/client/clientset/versioned/typed/serving/v1/fake" | ||
) | ||
|
||
const ( | ||
testNs = "test-ns" | ||
) | ||
|
||
var ( | ||
testSvc1 = v12.Service{ | ||
TypeMeta: metav1.TypeMeta{ | ||
Kind: "Service", | ||
APIVersion: "serving.knative.dev/v1", | ||
}, | ||
ObjectMeta: metav1.ObjectMeta{Name: "test-svc-1", Namespace: testNs}, | ||
} | ||
testSvc2 = v12.Service{ | ||
TypeMeta: metav1.TypeMeta{ | ||
Kind: "Service", | ||
APIVersion: "serving.knative.dev/v1", | ||
}, | ||
ObjectMeta: metav1.ObjectMeta{Name: "test-svc-2", Namespace: testNs}, | ||
} | ||
testSvc3 = v12.Service{ | ||
TypeMeta: metav1.TypeMeta{ | ||
Kind: "Service", | ||
APIVersion: "serving.knative.dev/v1", | ||
}, | ||
ObjectMeta: metav1.ObjectMeta{Name: "test-svc-3", Namespace: testNs}, | ||
} | ||
testNsServices = []v12.Service{testSvc1, testSvc2, testSvc3} | ||
|
||
fakeServing = &servingv1fake.FakeServingV1{Fake: &clienttesting.Fake{}} | ||
knParams = &KnParams{ | ||
NewServingClient: func(namespace string) (v1.KnServingClient, error) { | ||
return v1.NewKnServingClient(fakeServing, namespace), nil | ||
}, | ||
NewGitopsServingClient: func(namespace string, dir string) (v1.KnServingClient, error) { | ||
return v1.NewKnServingGitOpsClient(namespace, dir), nil | ||
}, | ||
} | ||
) | ||
|
||
func Test_completeService(t *testing.T) { | ||
|
||
fakeServing.AddReactor("list", "services", | ||
func(a clienttesting.Action) (bool, runtime.Object, error) { | ||
return true, &v12.ServiceList{Items: testNsServices}, nil | ||
}) | ||
tests := []struct { | ||
name string | ||
namespace string | ||
args []string | ||
p *KnParams | ||
toComplete string | ||
expectedSuggestions []string | ||
}{ | ||
{ | ||
"Suggestions when test-ns namespace set", | ||
testNs, | ||
nil, | ||
knParams, | ||
"", | ||
[]string{"test-svc-1", "test-svc-2", "test-svc-3"}, | ||
}, | ||
{ | ||
"Empty suggestions when non zero args", | ||
testNs, | ||
[]string{"xyz"}, | ||
knParams, | ||
"", | ||
[]string{}, | ||
}, | ||
{ | ||
"Empty suggestions when toComplete is not valid for completion", | ||
testNs, | ||
nil, | ||
knParams, | ||
"xyz", | ||
[]string{}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
testCmd := testCommandGenerator(false) | ||
testCmd.SetArgs([]string{"--namespace", tt.namespace}) | ||
testCmd.Execute() | ||
actualSuggestions, directive := completeService(tt.p, testCmd, tt.args, tt.toComplete) | ||
assert.DeepEqual(t, tt.expectedSuggestions, actualSuggestions) | ||
assert.Equal(t, cobra.ShellCompDirectiveNoFileComp, directive) | ||
} | ||
} | ||
|
||
func Test_completeGitOps(t *testing.T) { | ||
tempDir := setupTempDir(t) | ||
assert.Assert(t, tempDir != "") | ||
defer os.RemoveAll(tempDir) | ||
|
||
tests := []struct { | ||
name string | ||
namespace string | ||
args []string | ||
p *KnParams | ||
toComplete string | ||
expectedSuggestions []string | ||
}{ | ||
{ | ||
"Suggestions when test-ns namespace set", | ||
testNs, | ||
nil, | ||
knParams, | ||
"", | ||
[]string{"test-svc-1", "test-svc-2", "test-svc-3"}, | ||
}, | ||
{ | ||
"Empty suggestions when non zero args", | ||
testNs, | ||
[]string{"xyz"}, | ||
knParams, | ||
"", | ||
[]string{}, | ||
}, | ||
{ | ||
"Empty suggestions when toComplete is not valid for completion", | ||
testNs, | ||
nil, | ||
knParams, | ||
"xyz", | ||
[]string{}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
testCmd := testCommandGenerator(false) | ||
testCmd.Flags().String("target", "", "") | ||
testCmd.SetArgs([]string{"--namespace", tt.namespace, "--target", tempDir}) | ||
testCmd.Execute() | ||
actualSuggestions, directive := completeGitOps(tt.p, testCmd, tt.args, tt.toComplete, tempDir) | ||
assert.DeepEqual(t, tt.expectedSuggestions, actualSuggestions) | ||
assert.Equal(t, cobra.ShellCompDirectiveNoFileComp, directive) | ||
} | ||
} | ||
|
||
func setupTempDir(t *testing.T) string { | ||
tempDir, err := ioutil.TempDir("", "test-dir") | ||
assert.NilError(t, err) | ||
|
||
svcPath := path.Join(tempDir, "test-ns", "ksvc") | ||
err = os.MkdirAll(svcPath, 0700) | ||
assert.NilError(t, err) | ||
|
||
for i, testSvc := range []v12.Service{testSvc1, testSvc2, testSvc3} { | ||
tempFile, err := os.Create(path.Join(svcPath, fmt.Sprintf("test-svc-%d.yaml", i+1))) | ||
assert.NilError(t, err) | ||
writeToFile(t, testSvc, tempFile) | ||
} | ||
|
||
return tempDir | ||
} | ||
|
||
func writeToFile(t *testing.T, testSvc v12.Service, tempFile *os.File) { | ||
yamlPrinter, err := genericclioptions.NewJSONYamlPrintFlags().ToPrinter("yaml") | ||
assert.NilError(t, err) | ||
|
||
err = yamlPrinter.PrintObj(&testSvc, tempFile) | ||
assert.NilError(t, err) | ||
|
||
defer tempFile.Close() | ||
} |
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