github.phpd.cn/hashicorp/packer@v1.3.2/builder/oracle/classic/config_test.go (about)

     1  package classic
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  )
     8  
     9  func testConfig() map[string]interface{} {
    10  	return map[string]interface{}{
    11  		"identity_domain":   "abc12345",
    12  		"username":          "test@hashicorp.com",
    13  		"password":          "testpassword123",
    14  		"api_endpoint":      "https://api-test.compute.test.oraclecloud.com/",
    15  		"dest_image_list":   "/Config-thing/myuser/myimage",
    16  		"source_image_list": "/oracle/public/whatever",
    17  		"shape":             "oc3",
    18  		"image_name":        "TestImageName",
    19  		"ssh_username":      "opc",
    20  	}
    21  }
    22  
    23  func TestConfigAutoFillsSourceList(t *testing.T) {
    24  	tc := testConfig()
    25  	conf, err := NewConfig(tc)
    26  	if err != nil {
    27  		t.Fatalf("Should not have error: %s", err.Error())
    28  	}
    29  	if conf.SSHSourceList != "seciplist:/oracle/public/public-internet" {
    30  		t.Fatalf("conf.SSHSourceList should have been "+
    31  			"\"seciplist:/oracle/public/public-internet\" but is \"%s\"",
    32  			conf.SSHSourceList)
    33  	}
    34  }
    35  
    36  func TestConfigValidationCatchesMissing(t *testing.T) {
    37  	required := []string{
    38  		"username",
    39  		"password",
    40  		"api_endpoint",
    41  		"identity_domain",
    42  		"dest_image_list",
    43  		"source_image_list",
    44  		"shape",
    45  	}
    46  	for _, key := range required {
    47  		tc := testConfig()
    48  		delete(tc, key)
    49  		_, err := NewConfig(tc)
    50  		if err == nil {
    51  			t.Fatalf("Test should have failed when config lacked %s!", key)
    52  		}
    53  	}
    54  }
    55  
    56  func TestValidationsIgnoresOptional(t *testing.T) {
    57  	tc := testConfig()
    58  	delete(tc, "ssh_username")
    59  	_, err := NewConfig(tc)
    60  	if err != nil {
    61  		t.Fatalf("Shouldn't care if ssh_username is missing: err: %#v", err.Error())
    62  	}
    63  }
    64  
    65  func TestConfigValidatesObjects(t *testing.T) {
    66  	var objectTests = []struct {
    67  		object string
    68  		valid  bool
    69  	}{
    70  		{"foo-BAR.0_9", true},
    71  		{"%", false},
    72  		{"Matt...?", false},
    73  		{"/Config-thing/myuser/myimage", true},
    74  	}
    75  	for _, s := range []string{"dest_image_list", "image_name"} {
    76  		for _, tt := range objectTests {
    77  			tc := testConfig()
    78  			tc[s] = tt.object
    79  			_, err := NewConfig(tc)
    80  			if tt.valid {
    81  				assert.NoError(t, err, tt.object)
    82  			} else {
    83  				assert.Error(t, err, tt.object)
    84  			}
    85  		}
    86  	}
    87  }