github.com/containers/podman/v4@v4.9.4/pkg/machine/qemu/machine_test.go (about)

     1  //go:build (amd64 && !windows) || (arm64 && !windows)
     2  // +build amd64,!windows arm64,!windows
     3  
     4  package qemu
     5  
     6  import (
     7  	"encoding/base64"
     8  	"fmt"
     9  	"strings"
    10  	"testing"
    11  
    12  	"github.com/containers/common/libnetwork/etchosts"
    13  	"github.com/containers/podman/v4/pkg/machine"
    14  	"github.com/stretchr/testify/assert"
    15  	"github.com/stretchr/testify/require"
    16  )
    17  
    18  func TestEditCmd(t *testing.T) {
    19  	vm := new(MachineVM)
    20  	vm.CmdLine = QemuCmd{"command", "-flag", "value"}
    21  
    22  	vm.editCmdLine("-flag", "newvalue")
    23  	vm.editCmdLine("-anotherflag", "anothervalue")
    24  
    25  	require.Equal(t, vm.CmdLine.Build(), []string{"command", "-flag", "newvalue", "-anotherflag", "anothervalue"})
    26  }
    27  
    28  func TestPropagateHostEnv(t *testing.T) {
    29  	tests := map[string]struct {
    30  		value  string
    31  		expect string
    32  	}{
    33  		"HTTP_PROXY": {
    34  			"proxy",
    35  			"equal",
    36  		},
    37  		"ftp_proxy": {
    38  			"domain.com:8888",
    39  			"equal",
    40  		},
    41  		"FTP_PROXY": {
    42  			"proxy",
    43  			"equal",
    44  		},
    45  		"NO_PROXY": {
    46  			"localaddress",
    47  			"equal",
    48  		},
    49  		"HTTPS_PROXY": {
    50  			"",
    51  			"unset",
    52  		},
    53  		"no_proxy": {
    54  			"",
    55  			"unset",
    56  		},
    57  		"http_proxy": {
    58  			"127.0.0.1:8888",
    59  			fmt.Sprintf("%s:8888", etchosts.HostContainersInternal),
    60  		},
    61  		"https_proxy": {
    62  			"localhost:8888",
    63  			fmt.Sprintf("%s:8888", etchosts.HostContainersInternal),
    64  		},
    65  		"SSL_CERT_FILE": {
    66  			"/some/f=oo.cert",
    67  			fmt.Sprintf("%s/f=oo.cert", machine.UserCertsTargetPath),
    68  		},
    69  		"SSL_CERT_DIR": {
    70  			"/some/my/certs",
    71  			machine.UserCertsTargetPath,
    72  		},
    73  	}
    74  
    75  	for key, item := range tests {
    76  		t.Setenv(key, item.value)
    77  	}
    78  
    79  	cmdLine := propagateHostEnv(make([]string, 0))
    80  
    81  	assert.Len(t, cmdLine, 2)
    82  	assert.Equal(t, "-fw_cfg", cmdLine[0])
    83  	tokens := strings.Split(cmdLine[1], ",string=")
    84  	decodeString, err := base64.StdEncoding.DecodeString(tokens[1])
    85  	assert.NoError(t, err)
    86  
    87  	// envsRawArr looks like: ["BAR=\"bar\"", "FOO=\"foo\""]
    88  	envsRawArr := strings.Split(string(decodeString), "|")
    89  	// envs looks like: {"BAR": "bar", "FOO": "foo"}
    90  	envs := make(map[string]string)
    91  	for _, env := range envsRawArr {
    92  		item := strings.SplitN(env, "=", 2)
    93  		envs[item[0]] = strings.Trim(item[1], "\"")
    94  	}
    95  
    96  	for key, test := range tests {
    97  		switch test.expect {
    98  		case "equal":
    99  			assert.Equal(t, envs[key], test.value)
   100  		case "unset":
   101  			if _, ok := envs[key]; ok {
   102  				t.Errorf("env %s should not be set", key)
   103  			}
   104  		default:
   105  			assert.Equal(t, envs[key], test.expect)
   106  		}
   107  	}
   108  }