pulumi/pkg/backend/httpstate/backend_test.go

127 lines
3.2 KiB
Go

// Copyright 2016-2021, Pulumi Corporation.
//
// 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 httpstate
import (
"context"
"errors"
"testing"
"github.com/stretchr/testify/assert"
)
//nolint:paralleltest // mutates environment variables
func TestValueOrDefaultURL(t *testing.T) {
t.Run("TestValueOrDefault", func(t *testing.T) {
// Validate trailing slash gets cut
assert.Equal(t, "https://api-test1.pulumi.com", ValueOrDefaultURL("https://api-test1.pulumi.com/"))
// Validate no-op case
assert.Equal(t, "https://api-test2.pulumi.com", ValueOrDefaultURL("https://api-test2.pulumi.com"))
// Validate trailing slash in pre-set env var is unchanged
t.Setenv("PULUMI_API", "https://api-test3.pulumi.com/")
assert.Equal(t, "https://api-test3.pulumi.com/", ValueOrDefaultURL(""))
})
}
// TestDefaultOrganizationPriority tests the priority of the default organization.
// The priority is:
// 1. The default organization.
// 2. The user's organization.
func TestDefaultOrganizationPriority(t *testing.T) {
t.Parallel()
tests := []struct {
name string
getDefaultOrg func() (string, error)
getUserOrg func() (string, error)
wantOrg string
wantErr bool
}{
{
name: "default org set",
getDefaultOrg: func() (string, error) {
return "default-org", nil
},
getUserOrg: func() (string, error) {
return "", nil
},
wantOrg: "default-org",
},
{
name: "user org set",
getDefaultOrg: func() (string, error) {
return "", nil
},
getUserOrg: func() (string, error) {
return "user-org", nil
},
wantOrg: "user-org",
},
{
name: "no org set",
getDefaultOrg: func() (string, error) {
return "", nil
},
getUserOrg: func() (string, error) {
return "", nil
},
wantErr: true,
},
{
name: "both orgs set",
getDefaultOrg: func() (string, error) {
return "default-org", nil
},
getUserOrg: func() (string, error) {
return "user-org", nil
},
wantOrg: "default-org",
},
{
name: "default org set, user org error",
getDefaultOrg: func() (string, error) {
return "default-org", nil
},
getUserOrg: func() (string, error) {
return "", errors.New("user org error")
},
wantOrg: "default-org",
},
{
name: "user org set, default org error",
getDefaultOrg: func() (string, error) {
return "", errors.New("default org error")
},
getUserOrg: func() (string, error) {
return "user-org", nil
},
wantOrg: "user-org",
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
org, err := inferOrg(context.Background(), tt.getDefaultOrg, tt.getUserOrg)
if tt.wantErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
assert.Equal(t, tt.wantOrg, org)
})
}
}