Skip to content

Commit

Permalink
fixes + added test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
sharmaansh21 authored and Anshul Sharma committed Dec 7, 2023
1 parent 0b14ad0 commit a4a2ac6
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 14 deletions.
8 changes: 5 additions & 3 deletions pkg/kn/commands/service/configuration_edit_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -500,11 +500,13 @@ func (p *ConfigurationEditFlags) Apply(
if len(knconfig.GlobalConfig.Profile(profileName).Annotations) > 0 {
annotations := knconfig.GlobalConfig.Profile(profileName).Annotations
profileAnnotations := make(util.StringMap)
profileAnnotations.Merge(annotations)
for _, value := range annotations {
profileAnnotations[value.Name] = value.Value
}
if deleteProfile {
var annotationsToRemove []string
for key := range annotations {
annotationsToRemove = append(annotationsToRemove, key)
for _, value := range annotations {
annotationsToRemove = append(annotationsToRemove, value.Name)
}
err = servinglib.UpdateRevisionTemplateAnnotations(template, map[string]string{}, annotationsToRemove)
} else {
Expand Down
86 changes: 84 additions & 2 deletions pkg/kn/commands/service/configuration_edit_flags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,15 @@
package service

import (
"os"
"path/filepath"
"testing"

"github.com/spf13/viper"
"gotest.tools/v3/assert"

"knative.dev/client/pkg/kn/commands"
"knative.dev/client/pkg/kn/config"
"knative.dev/client/pkg/util"
"knative.dev/serving/pkg/apis/autoscaling"
)
Expand Down Expand Up @@ -76,15 +81,92 @@ func TestScaleActivation(t *testing.T) {
assert.Equal(t, svc.Spec.Template.Annotations[autoscaling.ActivationScaleKey], "2")
}

func TestApplyProfileFlag(t *testing.T) {
var editFlags ConfigurationEditFlags
knParams := &commands.KnParams{}
cmd, _, _ := commands.CreateTestKnCommand(NewServiceCreateCommand(knParams), knParams)
configYaml := `
profiles:
istio:
labels:
- name: environment
value: "test"
annotations:
- name: sidecar.istio.io/inject
value: "true"
- name: sidecar.istio.io/rewriteAppHTTPProbers
value: "true"
- name: serving.knative.openshift.io/enablePassthrough
value: "true"
`
_, cleanup := setupConfig(t, configYaml)
defer cleanup()

editFlags.AddCreateFlags(cmd)

err := config.BootstrapConfig()
assert.NilError(t, err)

svc := createTestService("test-svc", []string{"test-svc-00001"}, goodConditions())
cmd.SetArgs([]string{"--profile", "istio"})
cmd.Execute()
editFlags.Apply(&svc, nil, cmd)
assert.Equal(t, svc.Spec.Template.Annotations["sidecar.istio.io/inject"], "true")
assert.Equal(t, svc.Spec.Template.Annotations["sidecar.istio.io/rewriteAppHTTPProbers"], "true")
assert.Equal(t, svc.Spec.Template.Annotations["serving.knative.openshift.io/enablePassthrough"], "true")
}

func TestApplyProfileFlagError(t *testing.T) {
var editFlags ConfigurationEditFlags
knParams := &commands.KnParams{}
cmd, _, _ := commands.CreateTestKnCommand(NewServiceCreateCommand(knParams), knParams)
configYaml := `
profiles:
istio:
labels:
- name: environment
value: "test"
annotations:
- name: sidecar.istio.io/inject
value: "true"
`
_, cleanup := setupConfig(t, configYaml)
defer cleanup()

editFlags.AddCreateFlags(cmd)
svc := createTestService("test-svc", []string{"test-svc-00001", "test-svc-00002"}, goodConditions())

err := config.BootstrapConfig()
assert.NilError(t, err)

svc := createTestService("test-svc", []string{"test-svc-00001"}, goodConditions())
cmd.SetArgs([]string{"--profile", "invalidprofile"})
cmd.Execute()
err := editFlags.Apply(&svc, nil, cmd)
err = editFlags.Apply(&svc, nil, cmd)
assert.Assert(t, util.ContainsAll(err.Error(), "profile", "invalidprofile"))
}

func setupConfig(t *testing.T, configContent string) (string, func()) {
tmpDir := t.TempDir()

// Avoid to be fooled by the things in the the real homedir
oldHome := os.Getenv("HOME")
os.Setenv("HOME", tmpDir)

// Save old args
backupArgs := os.Args

// WriteCache out a temporary configContent file
var cfgFile string
if configContent != "" {
cfgFile = filepath.Join(tmpDir, "config.yaml")
os.Args = []string{"kn", "--config", cfgFile}
err := os.WriteFile(cfgFile, []byte(configContent), 0644)
assert.NilError(t, err)
}
return cfgFile, func() {
// Cleanup everything
os.Setenv("HOME", oldHome)
os.Args = backupArgs
viper.Reset()
}
}
35 changes: 28 additions & 7 deletions pkg/kn/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,15 @@ plugins:
profiles:
istio:
labels:
environment: test
- name: environment
value: "test"
annotations:
sidecar.istio.io/inject: "true"
sidecar.istio.io/rewriteAppHTTPProbers: "true"
serving.knative.openshift.io/enablePassthrough: "true"
- name: sidecar.istio.io/inject
value: "true"
- name: sidecar.istio.io/rewriteAppHTTPProbers
value: "true"
- name: serving.knative.openshift.io/enablePassthrough
value: "true"
eventing:
sink-mappings:
- prefix: service
Expand All @@ -59,10 +63,27 @@ eventing:
assert.Equal(t, GlobalConfig.PluginsDir(), "/tmp")
assert.Equal(t, GlobalConfig.LookupPluginsInPath(), true)
assert.Equal(t, len(GlobalConfig.SinkMappings()), 1)
assert.Equal(t, len(GlobalConfig.Profile("istio").Annotations), 3)
assert.Equal(t, GlobalConfig.Profile("istio").Annotations["sidecar.istio.io/inject"], "true")
assert.DeepEqual(t, GlobalConfig.Profile("istio").Annotations, []NamedValue{
{
Name: "sidecar.istio.io/inject",
Value: "true",
},
{
Name: "sidecar.istio.io/rewriteAppHTTPProbers",
Value: "true",
},
{
Name: "serving.knative.openshift.io/enablePassthrough",
Value: "true",
},
})
assert.Equal(t, len(GlobalConfig.Profile("istio").Labels), 1)
assert.Equal(t, GlobalConfig.Profile("istio").Labels["environment"], "test")
assert.DeepEqual(t, GlobalConfig.Profile("istio").Labels, []NamedValue{
{
Name: "environment",
Value: "test",
},
})
assert.DeepEqual(t, (GlobalConfig.SinkMappings())[0], SinkMapping{
Prefix: "service",
Resource: "services",
Expand Down
9 changes: 7 additions & 2 deletions pkg/kn/config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,14 @@ type ChannelTypeMapping struct {
Version string
}

type NamedValue struct {
Name string `yaml:"name"`
Value string `yaml:"value"`
}

type Profile struct {
Annotations map[string]string `yaml:"annotations"`
Labels map[string]string `yaml:"labels"`
Annotations []NamedValue `yaml:"annotations"`
Labels []NamedValue `yaml:"annotations"`
}

// config Keys for looking up in viper
Expand Down

0 comments on commit a4a2ac6

Please sign in to comment.