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

Fix crash in 3259, add test #3275

Merged
Show file tree
Hide file tree
Changes from 2 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: 2 additions & 0 deletions deployment/helm/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ spec:
value: "/secrets/github-app/github_app_webhook_secret"
- name: "MINDER_PROVIDER_GITHUB_APP_FALLBACK_TOKEN_FILE"
value: "/secrets/github-app/github_app_fallback_token"
- name: "MINDER_FLAGS_GO_FEATURE_FILE_PATH"
value: "/flags/flags-config.yaml"
- name: "MINDER_WEBHOOK_CONFIG_WEBHOOK_SECRET_FILE"
value: "/secrets/app/github_repo_webhook_secret"
- name: "MINDER_WEBHOOK_CONFIG_PREVIOUS_WEBHOOK_SECRET_FILE"
Expand Down
1 change: 1 addition & 0 deletions internal/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ func OpenFeatureProviderFromFlags(ctx context.Context, cfg config.FlagsConfig) {
})
if err != nil {
zerolog.Ctx(ctx).Error().Err(err).Msg("Unable to create GoFeatureFlag provider")
flagProvider = nil // Need to explicitly reset the value, see #3259
}
}

Expand Down
99 changes: 99 additions & 0 deletions internal/flags/flags_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
//
// Copyright 2024 Stacklok, Inc.
//
// 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 flags containts utilities for managing feature flags.
package flags

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

"github.com/google/uuid"
"github.com/lestrrat-go/jwx/v2/jwt/openid"
"github.com/open-feature/go-sdk/openfeature"
"github.com/stacklok/minder/internal/auth"
"github.com/stacklok/minder/internal/engine"

config "github.com/stacklok/minder/internal/config/server"
)

func TestOpenFeatureProviderFromFlags(t *testing.T) {

Check failure on line 35 in internal/flags/flags_test.go

View workflow job for this annotation

GitHub Actions / lint / Go Lint

TestOpenFeatureProviderFromFlags's subtests should call t.Parallel (tparallel)
t.Parallel()

testFile := filepath.Join(t.TempDir(), "testfile.yaml")
tempFile, err := os.Create(testFile)

Check failure on line 39 in internal/flags/flags_test.go

View workflow job for this annotation

GitHub Actions / lint / Go Lint

G304: Potential file inclusion via variable (gosec)
if err != nil {
t.Fatalf("failed to create temp file: %v", err)
}
t.Cleanup(func() { tempFile.Close() })

Check failure on line 43 in internal/flags/flags_test.go

View workflow job for this annotation

GitHub Actions / lint / Go Lint

G104: Errors unhandled. (gosec)
configFile := `
idp_resolver:
variations:
FlagOn: true
FlagOff: false
defaultRule:
variation: FlagOn
`
if _, err := io.WriteString(tempFile, configFile); err != nil {
t.Fatalf("failed to write to temp file: %v", err)
}

tests := []struct {
name string
cfg config.FlagsConfig
expectedFlag bool
}{{
name: "No Config",
cfg: config.FlagsConfig{},
}, {
name: "No File",
cfg: config.FlagsConfig{
GoFeature: config.GoFeatureConfig{
FilePath: "non-existent-file",
},
},
Comment on lines +64 to +69
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the test that would fail (panic) before.

}, {
name: "File exists",
cfg: config.FlagsConfig{
GoFeature: config.GoFeatureConfig{
FilePath: testFile,
},
},
expectedFlag: true,
}}
for _, tt := range tests {

Check failure on line 79 in internal/flags/flags_test.go

View workflow job for this annotation

GitHub Actions / lint / Go Lint

Range statement for test TestOpenFeatureProviderFromFlags missing the call to method parallel in test Run (paralleltest)
t.Run(tt.name, func(t *testing.T) {
ctx := context.Background()
OpenFeatureProviderFromFlags(ctx, tt.cfg)

client := openfeature.NewClient("test")
userJWT := openid.New()
userJWT.Set("sub", "user-1")

Check failure on line 86 in internal/flags/flags_test.go

View workflow job for this annotation

GitHub Actions / lint / Go Lint

G104: Errors unhandled. (gosec)
ctx = auth.WithAuthTokenContext(ctx, userJWT)
ctx = engine.WithEntityContext(ctx, &engine.EntityContext{
Project: engine.Project{uuid.New()},

Check failure on line 89 in internal/flags/flags_test.go

View workflow job for this annotation

GitHub Actions / lint / Go Lint

composites: github.com/stacklok/minder/internal/engine.Project struct literal uses unkeyed fields (govet)
Provider: engine.Provider{"testing"},

Check failure on line 90 in internal/flags/flags_test.go

View workflow job for this annotation

GitHub Actions / lint / Go Lint

composites: github.com/stacklok/minder/internal/engine.Provider struct literal uses unkeyed fields (govet)
})

flagResult := Bool(ctx, client, IDPResolver)
if flagResult != tt.expectedFlag {
t.Errorf("expected %v, got %v", tt.expectedFlag, flagResult)
}
})
}
}
Loading