Skip to content

Commit

Permalink
Added test cases for profile check
Browse files Browse the repository at this point in the history
  • Loading branch information
sharmaansh21 authored and Anshul Sharma committed Dec 6, 2023
1 parent a476ba6 commit 9cff5ef
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 3 deletions.
17 changes: 17 additions & 0 deletions pkg/kn/commands/service/configuration_edit_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"knative.dev/serving/pkg/apis/config"

knconfig "knative.dev/client/pkg/kn/config"
knflags "knative.dev/client/pkg/kn/flags"
servinglib "knative.dev/client/pkg/serving"
"knative.dev/client/pkg/util"
Expand Down Expand Up @@ -57,6 +58,7 @@ type ConfigurationEditFlags struct {
ClusterLocal bool
ScaleInit int
TimeoutSeconds int64
Profile string

// Preferences about how to do the action.
LockToDigest bool
Expand Down Expand Up @@ -187,6 +189,11 @@ func (p *ConfigurationEditFlags) addSharedFlags(command *cobra.Command) {
"Duration in seconds that the request routing layer will wait for a request delivered to a "+""+
"container to begin replying")
p.markFlagMakesRevision("timeout")

command.Flags().StringVar(&p.Profile, "profile", "",
"The profile name to set. This will add the annotations related to profile to the service. "+
"To unset, specify the profile name followed by a \"-\" (e.g., name-).")
p.markFlagMakesRevision("profile")
}

// AddUpdateFlags adds the flags specific to update.
Expand Down Expand Up @@ -479,6 +486,16 @@ func (p *ConfigurationEditFlags) Apply(
service.Spec.Template.Spec.TimeoutSeconds = &p.TimeoutSeconds
}

if cmd.Flags().Changed("profile") {
if len(knconfig.GlobalConfig.Profile(p.Profile).Annotations) > 0 {
return fmt.Errorf("profile doesn't exist %s", p.Profile)
} else if len(knconfig.GlobalConfig.Profile(p.Profile).Annotations) == 0 {
return fmt.Errorf("profile %s doesn't contain any annotations.", p.Profile)
} else {
return fmt.Errorf("profile %s doesn't exist.", p.Profile)
}
}

return nil
}

Expand Down
26 changes: 26 additions & 0 deletions pkg/kn/commands/service/configuration_edit_flags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,29 @@ func TestScaleActivation(t *testing.T) {
assert.NilError(t, err)
assert.Equal(t, svc.Spec.Template.Annotations[autoscaling.ActivationScaleKey], "2")
}

func TestApplyProfileFlagError(t *testing.T) {
var editFlags ConfigurationEditFlags
knParams := &commands.KnParams{}
cmd, _, _ := commands.CreateTestKnCommand(NewServiceCreateCommand(knParams), knParams)

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

func TestApplyProfileFlagAnnotationError(t *testing.T) {
var editFlags ConfigurationEditFlags
knParams := &commands.KnParams{}
cmd, _, _ := commands.CreateTestKnCommand(NewServiceCreateCommand(knParams), knParams)

editFlags.AddCreateFlags(cmd)
svc := createTestService("test-svc", []string{"test-svc-00001", "test-svc-00002"}, goodConditions())
cmd.SetArgs([]string{"--profile", "istio"})
cmd.Execute()
err := editFlags.Apply(&svc, nil, cmd)
assert.Assert(t, util.ContainsAll(err.Error(), "profile", "istio", "doesn't contain any annotations"))
}
5 changes: 4 additions & 1 deletion pkg/kn/commands/service/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,10 @@ var create_example = `
# Create the service in offline mode instead of kubernetes cluster (Beta)
kn service create gitopstest -n test-ns --image knativesamples/helloworld --target=/user/knfiles
kn service create gitopstest --image knativesamples/helloworld --target=/user/knfiles/test.yaml
kn service create gitopstest --image knativesamples/helloworld --target=/user/knfiles/test.json`
kn service create gitopstest --image knativesamples/helloworld --target=/user/knfiles/test.json
# Create a service with profile
kn service create isoto --image knativesamples/helloworld --profile istio`

func NewServiceCreateCommand(p *commands.KnParams) *cobra.Command {
var editFlags ConfigurationEditFlags
Expand Down
2 changes: 0 additions & 2 deletions pkg/kn/config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,6 @@ type Profile struct {
Annotations map[string]string `yaml:"annotations"`
}

type Profiles map[string]Profile

// config Keys for looking up in viper
const (
keyFeaturesContextSharing = "features.context-sharing"
Expand Down

0 comments on commit 9cff5ef

Please sign in to comment.