github.com/hernad/nomad@v1.6.112/command/config_validate_test.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: MPL-2.0
     3  
     4  package command
     5  
     6  import (
     7  	"os"
     8  	"path/filepath"
     9  	"testing"
    10  
    11  	"github.com/hernad/nomad/ci"
    12  	"github.com/mitchellh/cli"
    13  	"github.com/shoenig/test/must"
    14  )
    15  
    16  func TestConfigValidateCommand_FailWithEmptyDir(t *testing.T) {
    17  	ci.Parallel(t)
    18  	fh := t.TempDir()
    19  
    20  	ui := cli.NewMockUi()
    21  	cmd := &ConfigValidateCommand{Meta: Meta{Ui: ui}}
    22  	args := []string{fh}
    23  
    24  	code := cmd.Run(args)
    25  	must.One(t, code)
    26  }
    27  
    28  func TestConfigValidateCommand_SucceedWithMinimalConfigFile(t *testing.T) {
    29  	ci.Parallel(t)
    30  	fh := t.TempDir()
    31  
    32  	fp := filepath.Join(fh, "config.hcl")
    33  	err := os.WriteFile(fp, []byte(`data_dir="/"
    34  	client {
    35  		enabled = true
    36  	}`), 0644)
    37  	must.NoError(t, err)
    38  
    39  	ui := cli.NewMockUi()
    40  	cmd := &ConfigValidateCommand{Meta: Meta{Ui: ui}}
    41  	args := []string{fh}
    42  
    43  	code := cmd.Run(args)
    44  	must.Zero(t, code)
    45  }
    46  
    47  func TestConfigValidateCommand_FailOnParseBadConfigFile(t *testing.T) {
    48  	ci.Parallel(t)
    49  	fh := t.TempDir()
    50  
    51  	fp := filepath.Join(fh, "config.hcl")
    52  	err := os.WriteFile(fp, []byte(`a: b`), 0644)
    53  	must.NoError(t, err)
    54  
    55  	ui := cli.NewMockUi()
    56  	cmd := &ConfigValidateCommand{Meta: Meta{Ui: ui}}
    57  	args := []string{fh}
    58  
    59  	code := cmd.Run(args)
    60  	must.One(t, code)
    61  }
    62  
    63  func TestConfigValidateCommand_FailOnValidateParsableConfigFile(t *testing.T) {
    64  	ci.Parallel(t)
    65  	fh := t.TempDir()
    66  
    67  	fp := filepath.Join(fh, "config.hcl")
    68  	err := os.WriteFile(fp, []byte(`data_dir="../" 
    69  	client {
    70  		enabled = true 
    71  	}`), 0644)
    72  	must.NoError(t, err)
    73  
    74  	ui := cli.NewMockUi()
    75  	cmd := &ConfigValidateCommand{Meta: Meta{Ui: ui}}
    76  	args := []string{fh}
    77  
    78  	code := cmd.Run(args)
    79  	must.One(t, code)
    80  }