github.com/coreos/rocket@v1.30.1-0.20200224141603-171c416fac02/rkt/rkt_test.go (about)

     1  // Copyright 2016 The rkt Authors
     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 main
    16  
    17  import (
    18  	"fmt"
    19  	"io/ioutil"
    20  	"os"
    21  	"path/filepath"
    22  	"testing"
    23  
    24  	"github.com/rkt/rkt/pkg/log"
    25  )
    26  
    27  func TestCalculateDataDir(t *testing.T) {
    28  	// Used in calculateDataDir.
    29  	// TODO(tmrts): Restructure this pkg, specifically by using dependency
    30  	// injection, to eliminate these work-arounds.
    31  	stderr = log.New(os.Stderr, "TestCalculateDataDir", globalFlags.Debug)
    32  
    33  	_, err := getConfig()
    34  	if err != nil {
    35  		panic(fmt.Errorf("getConfig() got error %q", err))
    36  	}
    37  
    38  	if cachedConfig == nil {
    39  		panic(fmt.Errorf("getConfig() should've set `cachedConfig`"))
    40  	}
    41  
    42  	dirFlag := cmdRkt.PersistentFlags().Lookup("dir")
    43  	dirFlag.Changed = false
    44  	defDirFlagVal := dirFlag.Value.String()
    45  	defCfgDataDir := cachedConfig.Paths.DataDir
    46  
    47  	resetConfigState := func() {
    48  		cmdRkt.PersistentFlags().Set("dir", defDirFlagVal)
    49  		dirFlag.Changed = false
    50  
    51  		cachedConfig.Paths.DataDir = defCfgDataDir
    52  	}
    53  
    54  	tmpDir, err := ioutil.TempDir("", "")
    55  	if err != nil {
    56  		panic(fmt.Errorf("ioutil.TempDir(%q, %q) got error %q", "", "", err))
    57  	}
    58  	defer os.Remove(tmpDir)
    59  
    60  	// TODO(tmrts): Write a utility function that generates unused random paths.
    61  	// Example signature would be fileutils.GenerateUniquePath(prefix string) (string, error).
    62  	nonExistentDir, err := ioutil.TempDir("", "non-existent-")
    63  	if err != nil {
    64  		panic(fmt.Errorf("ioutil.TempDir(%q, %q) got error %q", "", "", err))
    65  	}
    66  	if err := os.Remove(nonExistentDir); err != nil {
    67  		panic(fmt.Errorf("os.Remove(%q) got error %q", nonExistentDir, err))
    68  	}
    69  
    70  	testCases := []struct {
    71  		flagDataDir   string
    72  		configDataDir string
    73  		out           string
    74  	}{
    75  		{"", "", defaultDataDir},
    76  		{"", tmpDir, tmpDir},
    77  		{tmpDir, "", tmpDir},
    78  		{nonExistentDir, "", nonExistentDir},
    79  		{"", nonExistentDir, nonExistentDir},
    80  	}
    81  
    82  	for _, tc := range testCases {
    83  		cmdRkt.PersistentFlags().Set("dir", tc.flagDataDir)
    84  
    85  		cachedConfig.Paths.DataDir = tc.configDataDir
    86  
    87  		realDataDir, err := filepath.EvalSymlinks(tc.out)
    88  		if err != nil {
    89  			if os.IsNotExist(err) {
    90  				realDataDir = tc.out
    91  			} else {
    92  				panic(fmt.Errorf("filepath.EvalSymlinks(%q) got error %q", tc.out, err))
    93  			}
    94  		}
    95  
    96  		if dataDir := calculateDataDir(); dataDir != realDataDir {
    97  			t.Errorf("main.calculateDataDir() with setup %q, expected %q, got %q", tc, realDataDir, dataDir)
    98  		}
    99  
   100  		resetConfigState()
   101  	}
   102  }