github.com/goliatone/go-envset@v0.7.0/cmd/envset/main_test.go (about)

     1  package main
     2  
     3  import (
     4  	"crypto/md5"
     5  	"encoding/hex"
     6  	"io"
     7  	"os"
     8  	"path"
     9  	"testing"
    10  
    11  	"github.com/rendon/testcli"
    12  	"github.com/stretchr/testify/assert"
    13  )
    14  
    15  var bin string
    16  
    17  func init() {
    18  	cur, _ := os.Getwd()
    19  	bin = path.Join(cur, "envset")
    20  }
    21  
    22  func Test_CommandHelp(t *testing.T) {
    23  	testcli.Run(bin, "-h")
    24  	if !testcli.Success() {
    25  		t.Fatalf("Expected to succeed, but failed: %q with message: %q", testcli.Error(), testcli.Stderr())
    26  	}
    27  }
    28  
    29  func Test_Version(t *testing.T) {
    30  	testcli.Run(bin, "-v")
    31  
    32  	if !testcli.Success() {
    33  		t.Fatalf("Expected to succeed, but failed: %q with message: %q", testcli.Error(), testcli.Stderr())
    34  	}
    35  
    36  	if !testcli.StdoutContains("version") {
    37  		t.Fatalf("Expected %q to contain %q", testcli.Stdout(), "version?")
    38  	}
    39  }
    40  
    41  func Test_Print(t *testing.T) {
    42  	testcli.Run(bin, "--env-file=testdata/.envset")
    43  
    44  	if !testcli.Success() {
    45  		t.Fatalf("Expected to succeed, but failed: %q with message: %q", testcli.Error(), testcli.Stderr())
    46  	}
    47  
    48  	//TODO: how do we get the full stdout? it breaks on first \n
    49  	if !testcli.StdoutContains("GLOBAL") {
    50  		t.Fatalf("Expected %q to contain %q", testcli.Stdout(), "GLOBAL?")
    51  	}
    52  }
    53  
    54  func Test_ExecCmd(t *testing.T) {
    55  	dir := cd("testdata", t)
    56  
    57  	testcli.Run(bin,
    58  		"development",
    59  		"--",
    60  		"sh",
    61  		"test.sh",
    62  	)
    63  
    64  	if !testcli.Success() {
    65  		t.Fatalf("Expected to succeed, but failed: %q with message: %q", testcli.Error(), testcli.Stderr())
    66  	}
    67  
    68  	if !testcli.StdoutContains("out: envset_result") {
    69  		t.Fatalf("Expected %q to contain %q", testcli.Stdout(), "envset_result?")
    70  	}
    71  
    72  	cd(dir, t)
    73  }
    74  
    75  func Test_APP_ENV(t *testing.T) {
    76  	dir := cd("testdata", t)
    77  
    78  	testcli.Run(bin,
    79  		"development",
    80  		"--",
    81  		"sh",
    82  		"app_env.sh",
    83  	)
    84  
    85  	if !testcli.Success() {
    86  		t.Fatalf("Expected to succeed, but failed: %q with message: %q", testcli.Error(), testcli.Stderr())
    87  	}
    88  
    89  	if !testcli.StdoutContains("development") {
    90  		t.Fatalf("Expected %q to contain %q", testcli.Stdout(), "APP_ENV?")
    91  	}
    92  
    93  	cd(dir, t)
    94  }
    95  
    96  func Test_Metadata(t *testing.T) {
    97  	dir := cd("testdata", t)
    98  	rm(".meta", t)
    99  
   100  	testcli.Run(bin,
   101  		"metadata",
   102  	)
   103  
   104  	if !testcli.Success() {
   105  		t.Fatalf("Expected to succeed, but failed: %q with message: %q", testcli.Error(), testcli.Stderr())
   106  	}
   107  
   108  	assert.DirExists(t, ".meta")
   109  	assert.FileExists(t, path.Join(".meta", "data.json"))
   110  
   111  	rm(".meta", t)
   112  	cd(dir, t)
   113  }
   114  
   115  func Test_Metadata_Print(t *testing.T) {
   116  	dir := cd("testdata", t)
   117  	rm(".meta", t)
   118  
   119  	testcli.Run(bin,
   120  		"metadata",
   121  		"--print",
   122  	)
   123  
   124  	if !testcli.Success() {
   125  		t.Fatalf("Expected to succeed, but failed: %q with message: %q", testcli.Error(), testcli.Stderr())
   126  	}
   127  
   128  	if !testcli.StdoutContains("development") {
   129  		t.Fatalf("Expected %q to contain %q", testcli.Stdout(), "APP_ENV?")
   130  	}
   131  
   132  	if !testcli.StdoutContains("\"algorithm\": \"sha256\"") {
   133  		t.Fatalf("Expected %q to contain %q", testcli.Stdout(), "sha256 default algorithm?")
   134  	}
   135  
   136  	assert.NoDirExists(t, ".meta")
   137  	assert.NoFileExists(t, path.Join(".meta", "data.json"))
   138  
   139  	rm(".meta", t)
   140  	cd(dir, t)
   141  }
   142  
   143  func Test_Metadata_Secret_Print(t *testing.T) {
   144  	dir := cd("testdata", t)
   145  	rm(".meta", t)
   146  
   147  	testcli.Run(bin,
   148  		"metadata",
   149  		"--print",
   150  		"--secret=secret",
   151  	)
   152  
   153  	if !testcli.Success() {
   154  		t.Fatalf("Expected to succeed, but failed: %q with message: %q", testcli.Error(), testcli.Stderr())
   155  	}
   156  
   157  	if !testcli.StdoutContains("\"algorithm\": \"hmac\"") {
   158  		t.Fatalf("Expected %q to contain %q", testcli.Stdout(), "hmac algorithm?")
   159  	}
   160  	assert.NoDirExists(t, ".meta")
   161  	assert.NoFileExists(t, path.Join(".meta", "data.json"))
   162  
   163  	rm(".meta", t)
   164  	cd(dir, t)
   165  }
   166  
   167  func Test_Metadata_MD5_Print(t *testing.T) {
   168  	dir := cd("testdata", t)
   169  	rm(".meta", t)
   170  
   171  	testcli.Run(bin,
   172  		"metadata",
   173  		"--print",
   174  		"--hash-algo",
   175  		"md5",
   176  	)
   177  
   178  	if !testcli.Success() {
   179  		t.Fatalf("Expected to succeed, but failed: %q with message: %q", testcli.Error(), testcli.Stderr())
   180  	}
   181  
   182  	if !testcli.StdoutContains("\"algorithm\": \"md5\"") {
   183  		t.Fatalf("Expected %q to contain %q", testcli.Stdout(), "md5 algorithm?")
   184  	}
   185  	assert.NoDirExists(t, ".meta")
   186  	assert.NoFileExists(t, path.Join(".meta", "data.json"))
   187  
   188  	rm(".meta", t)
   189  	cd(dir, t)
   190  }
   191  
   192  func Test_Metadata_Idempotency(t *testing.T) {
   193  	dir := cd("testdata", t)
   194  	rm(".meta", t)
   195  
   196  	testcli.Run(bin,
   197  		"metadata",
   198  	)
   199  
   200  	if !testcli.Success() {
   201  		t.Fatalf("Expected to succeed, but failed: %q with message: %q", testcli.Error(), testcli.Stderr())
   202  	}
   203  
   204  	filePath := path.Join(".meta", "data.json")
   205  	assert.DirExists(t, ".meta")
   206  	assert.FileExists(t, filePath)
   207  
   208  	hash1 := md5sum(filePath, t)
   209  
   210  	testcli.Run(bin,
   211  		"metadata",
   212  	)
   213  
   214  	if !testcli.Success() {
   215  		t.Fatalf("Expected to succeed, but failed: %q with message: %q", testcli.Error(), testcli.Stderr())
   216  	}
   217  
   218  	hash2 := md5sum(filePath, t)
   219  
   220  	if hash1 != hash2 {
   221  		t.Fatal("Expected meta file to not change with no data changes")
   222  	}
   223  
   224  	rm(".meta", t)
   225  	cd(dir, t)
   226  }
   227  
   228  func Test_MetadataOptions(t *testing.T) {
   229  	rm("testdata/meta", t)
   230  
   231  	testcli.Run(bin,
   232  		"metadata",
   233  		"--env-file=testdata/.envset",
   234  		"--filepath=testdata/meta",
   235  		"--filename=data.json",
   236  	)
   237  
   238  	if !testcli.Success() {
   239  		t.Fatalf("Expected to succeed, but failed: %q with message: %q", testcli.Error(), testcli.Stderr())
   240  	}
   241  
   242  	assert.DirExists(t, path.Join("testdata", "meta"))
   243  	assert.FileExists(t, path.Join("testdata", "meta", "data.json"))
   244  
   245  	rm("testdata/meta", t)
   246  }
   247  
   248  func Test_DotEnvFile(t *testing.T) {
   249  	testcli.Run(bin, "--env-file=testdata/.env")
   250  
   251  	if !testcli.Success() {
   252  		t.Fatalf("Expected to succeed, but failed: %q with message: %q", testcli.Error(), testcli.Stderr())
   253  	}
   254  
   255  	if !testcli.StdoutContains("EXPECTED") {
   256  		t.Fatalf("Expected %q to contain %q", testcli.Stdout(), "EXPECTED?")
   257  	}
   258  }
   259  
   260  func Test_Template(t *testing.T) {
   261  	dir := cd("testdata", t)
   262  	rm("envset.example", t)
   263  
   264  	testcli.Run(bin, "template")
   265  
   266  	if !testcli.Success() {
   267  		t.Fatalf("Expected to succeed, but failed: %q with message: %q", testcli.Error(), testcli.Stderr())
   268  	}
   269  
   270  	assert.FileExists(t, "envset.example")
   271  
   272  	rm("envset.example", t)
   273  	cd(dir, t)
   274  }
   275  
   276  func Test_TemplateOptions(t *testing.T) {
   277  	dir := cd("testdata", t)
   278  	rm("output/env.tpl", t)
   279  
   280  	testcli.Run(bin,
   281  		"template",
   282  		"--filename=env.tpl",
   283  		"--filepath=output",
   284  		"--env-file=.env",
   285  	)
   286  
   287  	if !testcli.Success() {
   288  		t.Fatalf("Expected to succeed, but failed: %q with message: %q", testcli.Error(), testcli.Stderr())
   289  	}
   290  
   291  	assert.FileExists(t, path.Join("output", "env.tpl"))
   292  
   293  	rm("output", t)
   294  	cd(dir, t)
   295  }
   296  
   297  func rm(dir string, t *testing.T) {
   298  	err := os.RemoveAll(dir)
   299  	if err != nil {
   300  		t.Fatalf("error removing dir: %q ", err)
   301  	}
   302  }
   303  
   304  func cd(dir string, t *testing.T) string {
   305  	cur, err := os.Getwd()
   306  	if err != nil {
   307  		t.Fatalf("error cd dir: %q ", err)
   308  	}
   309  	os.Chdir(dir)
   310  	return cur
   311  }
   312  
   313  func md5sum(filePath string, t *testing.T) string {
   314  	file, err := os.Open(filePath)
   315  	if err != nil {
   316  		t.Fatalf("error removing dir: %q ", err)
   317  	}
   318  	defer file.Close()
   319  
   320  	hash := md5.New()
   321  	if _, err := io.Copy(hash, file); err != nil {
   322  		t.Fatalf("error removing dir: %q ", err)
   323  	}
   324  	return hex.EncodeToString(hash.Sum(nil))
   325  }