Skip to content

Commit

Permalink
Merge pull request #1899 from chenz4027/OCM-6840-test
Browse files Browse the repository at this point in the history
OCM-6840 | fix: Add json output test for external auth
  • Loading branch information
openshift-merge-bot[bot] committed Apr 5, 2024
2 parents 5935b9c + a958983 commit ebcb463
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 13 deletions.
2 changes: 1 addition & 1 deletion cmd/describe/externalauthprovider/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func runWithRuntime(r *rosa.Runtime, cmd *cobra.Command, argv []string) error {
r.Reporter.Errorf("%v", err)
os.Exit(1)
}
os.Exit(0)
return nil
}

externalAuthConfigList := describeExternalAuthProviders(r, cluster, clusterKey, externalAuthConfig)
Expand Down
59 changes: 53 additions & 6 deletions cmd/describe/externalauthprovider/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,28 @@ Issuer audiences:
Issuer Url: https://test.com
Claim mappings group: groups
Claim mappings username: username
`

jsonOutput = `{
"kind": "ExternalAuth",
"id": "microsoft-entra-id",
"claim": {
"mappings": {
"groups": {
"claim": "groups"
},
"username": {
"claim": "username"
}
}
},
"issuer": {
"url": "https://test.com",
"audiences": [
"abc"
]
}
}
`
)

Expand All @@ -39,8 +61,7 @@ var _ = Describe("External authentication provider", func() {
})
hypershiftClusterReady := test.FormatClusterList([]*cmv1.Cluster{mockClusterReady})

externalAuths := make([]*cmv1.ExternalAuth, 0)
externalAuths = append(externalAuths, test.BuildExternalAuth())
externalAuth := test.BuildExternalAuth()

BeforeEach(func() {
testRuntime.InitRuntime()
Expand Down Expand Up @@ -68,13 +89,39 @@ var _ = Describe("External authentication provider", func() {
Expect(stderr).To(BeEmpty())
})

It("Pass an external auth provider name through parameter with -o json but it is not found", func() {
Cmd.Flags().Set("output", "json")
args.name = externalAuthName
testRuntime.ApiServer.AppendHandlers(RespondWithJSON(http.StatusOK, hypershiftClusterReady))
testRuntime.ApiServer.AppendHandlers(RespondWithJSON(http.StatusNotFound, ""))
stdout, stderr, err := test.RunWithOutputCaptureAndArgv(runWithRuntime, testRuntime.RosaRuntime,
Cmd, &[]string{})
Expect(err).ToNot(BeNil())
Expect(err.Error()).To(ContainSubstring(
fmt.Sprintf("external authentication provider '%s' not found", externalAuthName)))
Expect(stdout).To(BeEmpty())
Expect(stderr).To(BeEmpty())
})

It("Pass an external auth provider name through parameter and it is found.", func() {
args.name = externalAuthName
testRuntime.ApiServer.AppendHandlers(RespondWithJSON(http.StatusOK, hypershiftClusterReady))
testRuntime.ApiServer.AppendHandlers(RespondWithJSON(http.StatusOK, test.FormatExternalAuthList(externalAuths)))
output := describeExternalAuthProviders(testRuntime.RosaRuntime,
mockClusterReady, test.MockClusterID, test.BuildExternalAuth())
Expect(output).To(Equal(describeStringOutput))
testRuntime.ApiServer.AppendHandlers(RespondWithJSON(http.StatusOK, test.FormatResource(externalAuth)))
stdout, stderr, err := test.RunWithOutputCaptureAndArgv(runWithRuntime, testRuntime.RosaRuntime, Cmd, &[]string{})
Expect(err).To(BeNil())
Expect(stderr).To(BeEmpty())
Expect(stdout).To(Equal(describeStringOutput))
})

It("Pass an external auth provider name through parameter with -o json and it is found.", func() {
Cmd.Flags().Set("output", "json")
args.name = externalAuthName
testRuntime.ApiServer.AppendHandlers(RespondWithJSON(http.StatusOK, hypershiftClusterReady))
testRuntime.ApiServer.AppendHandlers(RespondWithJSON(http.StatusOK, test.FormatResource(externalAuth)))
stdout, stderr, err := test.RunWithOutputCaptureAndArgv(runWithRuntime, testRuntime.RosaRuntime, Cmd, &[]string{})
Expect(err).To(BeNil())
Expect(stderr).To(BeEmpty())
Expect(stdout).To(Equal(jsonOutput))
})
})
})
5 changes: 2 additions & 3 deletions cmd/list/externalauthprovider/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,9 @@ func runWithRuntime(r *rosa.Runtime, cmd *cobra.Command) error {
if output.HasFlag() {
err = output.Print(externalAuthProviders)
if err != nil {
r.Reporter.Errorf("%s", err)
os.Exit(1)
return fmt.Errorf("%s", err)
}
os.Exit(0)
return nil
}

if len(externalAuthProviders) == 0 {
Expand Down
54 changes: 52 additions & 2 deletions cmd/list/externalauthprovider/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,36 @@ import (
"github.com/openshift/rosa/pkg/test"
)

const (
listOutput = `NAME ISSUER URL
microsoft-entra-id https://test.com
`

jsonOutput = `[
{
"kind": "ExternalAuth",
"id": "microsoft-entra-id",
"claim": {
"mappings": {
"groups": {
"claim": "groups"
},
"username": {
"claim": "username"
}
}
},
"issuer": {
"url": "https://test.com",
"audiences": [
"abc"
]
}
}
]
`
)

var _ = Describe("list external-auth-provider", func() {

var testRuntime test.TestingRuntime
Expand Down Expand Up @@ -49,10 +79,30 @@ var _ = Describe("list external-auth-provider", func() {
It("Succeeds", func() {
testRuntime.ApiServer.AppendHandlers(RespondWithJSON(http.StatusOK, hypershiftClusterReady))
testRuntime.ApiServer.AppendHandlers(RespondWithJSON(http.StatusOK, test.FormatExternalAuthList(externalAuths)))
_, _, err := test.RunWithOutputCapture(runWithRuntime, testRuntime.RosaRuntime, Cmd)
stdout, stderr, err := test.RunWithOutputCapture(runWithRuntime, testRuntime.RosaRuntime, Cmd)
Expect(err).To(BeNil())
Expect(stderr).To(BeEmpty())
Expect(stdout).To(Equal(listOutput))
})

})
It("Returns empty array with -o json if not found", func() {
Cmd.Flags().Set("output", "json")
testRuntime.ApiServer.AppendHandlers(RespondWithJSON(http.StatusOK, hypershiftClusterReady))
testRuntime.ApiServer.AppendHandlers(RespondWithJSON(http.StatusNotFound, ""))
stdout, stderr, err := test.RunWithOutputCapture(runWithRuntime, testRuntime.RosaRuntime, Cmd)
Expect(err).To(BeNil())
Expect(stderr).To(BeEmpty())
Expect(stdout).To(Equal("[]\n"))
})

It("Returns array with json context with -o json if found", func() {
Cmd.Flags().Set("output", "json")
testRuntime.ApiServer.AppendHandlers(RespondWithJSON(http.StatusOK, hypershiftClusterReady))
testRuntime.ApiServer.AppendHandlers(RespondWithJSON(http.StatusOK, test.FormatExternalAuthList(externalAuths)))
stdout, stderr, err := test.RunWithOutputCapture(runWithRuntime, testRuntime.RosaRuntime, Cmd)
Expect(err).To(BeNil())
Expect(stderr).To(BeEmpty())
Expect(stdout).To(Equal(jsonOutput))
})
})
})
2 changes: 1 addition & 1 deletion cmd/revoke/breakglasscredential/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func runWithRuntime(r *rosa.Runtime, cmd *cobra.Command, argv []string) error {
return fmt.Errorf("failed to revoke break glass credentials on cluster '%s': %s",
clusterKey, err)
}
r.Reporter.Infof("Successfully revoked all break glass credentials from cluster '%s'",
r.Reporter.Infof("Successfully requested revocation for all break glass credentials from cluster '%s'",
clusterKey)
}
return nil
Expand Down
4 changes: 4 additions & 0 deletions pkg/test/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,10 @@ func FormatResource(resource interface{}) string {
if res, ok := resource.(*v1.ControlPlaneUpgradePolicy); ok {
err = v1.MarshalControlPlaneUpgradePolicy(res, &outputJson)
}
case "*v1.ExternalAuth":
if res, ok := resource.(*v1.ExternalAuth); ok {
err = v1.MarshalExternalAuth(res, &outputJson)
}
default:
{
return "NOTIMPLEMENTED"
Expand Down

0 comments on commit ebcb463

Please sign in to comment.