Skip to content
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

OCM-6840 | fix: Add json output test for external auth #1899

Merged
merged 1 commit into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -66,7 +66,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