github.com/yourbase/yb@v0.7.1/cmd/yb/helpers_test.go (about)

     1  // Copyright 2021 YourBase Inc.
     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  //		 https://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  // SPDX-License-Identifier: Apache-2.0
    16  
    17  package main
    18  
    19  import (
    20  	"fmt"
    21  	"os"
    22  	"strings"
    23  	"testing"
    24  
    25  	"github.com/yourbase/yb"
    26  )
    27  
    28  func TestWillUseDocker(t *testing.T) {
    29  	const containerEnvVar = "YB_CONTAINER_ENV_IP"
    30  	oldEnv, oldEnvExists := os.LookupEnv(containerEnvVar)
    31  	os.Setenv(containerEnvVar, "1.2.3.4")
    32  	t.Cleanup(func() {
    33  		if !oldEnvExists {
    34  			os.Unsetenv(containerEnvVar)
    35  		} else {
    36  			os.Setenv(containerEnvVar, oldEnv)
    37  		}
    38  	})
    39  
    40  	networkAvailable, _ := hostHasDockerNetwork()
    41  	tests := []struct {
    42  		mode        executionMode
    43  		targets     []*yb.Target
    44  		want        bool
    45  		forCommands bool
    46  	}{
    47  		{
    48  			mode: noContainer,
    49  			targets: []*yb.Target{
    50  				{Name: "default", UseContainer: false},
    51  				{Name: "foo", UseContainer: false},
    52  			},
    53  			want:        false,
    54  			forCommands: false,
    55  		},
    56  		{
    57  			mode: preferHost,
    58  			targets: []*yb.Target{
    59  				{Name: "default", UseContainer: false},
    60  				{Name: "foo", UseContainer: false},
    61  			},
    62  			want:        false,
    63  			forCommands: false,
    64  		},
    65  		{
    66  			mode: useContainer,
    67  			targets: []*yb.Target{
    68  				{Name: "default", UseContainer: false},
    69  				{Name: "foo", UseContainer: false},
    70  			},
    71  			want:        true,
    72  			forCommands: true,
    73  		},
    74  		{
    75  			mode: noContainer,
    76  			targets: []*yb.Target{
    77  				{Name: "default", UseContainer: true},
    78  				{Name: "foo", UseContainer: false},
    79  			},
    80  			want:        true,
    81  			forCommands: true,
    82  		},
    83  		{
    84  			mode: preferHost,
    85  			targets: []*yb.Target{
    86  				{Name: "default", UseContainer: true},
    87  				{Name: "foo", UseContainer: false},
    88  			},
    89  			want:        true,
    90  			forCommands: true,
    91  		},
    92  		{
    93  			mode: useContainer,
    94  			targets: []*yb.Target{
    95  				{Name: "default", UseContainer: true},
    96  				{Name: "foo", UseContainer: false},
    97  			},
    98  			want:        true,
    99  			forCommands: true,
   100  		},
   101  		{
   102  			mode: noContainer,
   103  			targets: []*yb.Target{
   104  				{Name: "default", UseContainer: false, Resources: map[string]*yb.ResourceDefinition{"foo": {}}},
   105  				{Name: "foo", UseContainer: false},
   106  			},
   107  			want:        true,
   108  			forCommands: !networkAvailable,
   109  		},
   110  		{
   111  			mode: preferHost,
   112  			targets: []*yb.Target{
   113  				{Name: "default", UseContainer: false, Resources: map[string]*yb.ResourceDefinition{"foo": {}}},
   114  				{Name: "foo", UseContainer: false},
   115  			},
   116  			want:        true,
   117  			forCommands: !networkAvailable,
   118  		},
   119  		{
   120  			mode: preferHost,
   121  			targets: []*yb.Target{
   122  				// This is provided as YB_CONTAINER_ENV_IP.
   123  				{Name: "default", UseContainer: false, Resources: map[string]*yb.ResourceDefinition{"env": {}}},
   124  				{Name: "foo", UseContainer: false},
   125  			},
   126  			want:        false,
   127  			forCommands: false,
   128  		},
   129  		{
   130  			mode: useContainer,
   131  			targets: []*yb.Target{
   132  				{Name: "default", UseContainer: false, Resources: map[string]*yb.ResourceDefinition{"foo": {}}},
   133  				{Name: "foo", UseContainer: false},
   134  			},
   135  			want:        true,
   136  			forCommands: true,
   137  		},
   138  	}
   139  
   140  	formatTargets := func(targets []*yb.Target) string {
   141  		stringList := make([]string, 0, len(targets))
   142  		for _, tgt := range targets {
   143  			stringList = append(stringList, fmt.Sprintf("{Name:%q UseContainer:%t Resources:%+v}", tgt.Name, tgt.UseContainer, tgt.Resources))
   144  		}
   145  		return "[" + strings.Join(stringList, " ") + "]"
   146  	}
   147  
   148  	for _, test := range tests {
   149  		if got := willUseDocker(test.mode, test.targets); got != test.want {
   150  			t.Errorf("willUseDocker(%d, %s) = %t; want %t", test.mode, formatTargets(test.targets), got, test.want)
   151  		}
   152  		if got := willUseDockerForCommands(test.mode, test.targets); got != test.forCommands {
   153  			t.Errorf("willUseDockerForCommands(%d, %s) = %t; want %t", test.mode, formatTargets(test.targets), got, test.forCommands)
   154  		}
   155  	}
   156  }