...

Source file src/cloud.google.com/go/auth/internal/internal_test.go

Documentation: cloud.google.com/go/auth/internal

     1  // Copyright 2024 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package internal
    16  
    17  import (
    18  	"context"
    19  	"errors"
    20  	"testing"
    21  
    22  	"cloud.google.com/go/compute/metadata"
    23  )
    24  
    25  func TestComputeUniverseDomainProvider(t *testing.T) {
    26  	fatalErr := errors.New("fatal error")
    27  	notDefinedError := metadata.NotDefinedError("universe/universe_domain")
    28  	testCases := []struct {
    29  		name    string
    30  		getFunc func(ctx context.Context) (string, error)
    31  		want    string
    32  		wantErr error
    33  	}{
    34  		{
    35  			name: "test error",
    36  			getFunc: func(ctx context.Context) (string, error) {
    37  				return "", fatalErr
    38  			},
    39  			want:    "",
    40  			wantErr: fatalErr,
    41  		},
    42  		{
    43  			name: "test error 404",
    44  			getFunc: func(ctx context.Context) (string, error) {
    45  				return "", notDefinedError
    46  			},
    47  			want:    DefaultUniverseDomain,
    48  			wantErr: nil,
    49  		},
    50  		{
    51  			name: "test valid",
    52  			getFunc: func(ctx context.Context) (string, error) {
    53  				return "example.com", nil
    54  			},
    55  			want:    "example.com",
    56  			wantErr: nil,
    57  		},
    58  	}
    59  
    60  	oldGet := httpGetMetadataUniverseDomain
    61  	defer func() {
    62  		httpGetMetadataUniverseDomain = oldGet
    63  	}()
    64  	for _, tc := range testCases {
    65  		t.Run(tc.name, func(t *testing.T) {
    66  			httpGetMetadataUniverseDomain = tc.getFunc
    67  			c := ComputeUniverseDomainProvider{}
    68  			got, err := c.GetProperty(context.Background())
    69  			if err != tc.wantErr {
    70  				t.Errorf("got error %v; want error %v", err, tc.wantErr)
    71  			}
    72  			if got != tc.want {
    73  				t.Errorf("got %v; want %v", got, tc.want)
    74  			}
    75  		})
    76  	}
    77  }
    78  

View as plain text