github.com/apprenda/kismatic@v1.12.0/pkg/ansible/inventory_test.go (about)

     1  package ansible
     2  
     3  import "testing"
     4  
     5  func TestInventoryINIGeneration(t *testing.T) {
     6  	inv := Inventory{
     7  		Roles: []Role{
     8  			{
     9  				Name: "etcd",
    10  				Nodes: []Node{
    11  					{
    12  						Host:          "etcd01",
    13  						PublicIP:      "10.0.0.1",
    14  						InternalIP:    "192.168.0.11",
    15  						SSHPrivateKey: "id_rsa",
    16  						SSHPort:       2222,
    17  						SSHUser:       "alice",
    18  					},
    19  				},
    20  			},
    21  			{
    22  				Name: "master",
    23  				Nodes: []Node{
    24  					{
    25  						Host:          "master01",
    26  						PublicIP:      "10.0.0.2",
    27  						InternalIP:    "192.168.0.12",
    28  						SSHPrivateKey: "id_rsa",
    29  						SSHPort:       2222,
    30  						SSHUser:       "alice",
    31  					},
    32  				},
    33  			}, {
    34  				Name: "worker",
    35  				Nodes: []Node{
    36  					{
    37  						Host:          "worker01",
    38  						PublicIP:      "10.0.0.3",
    39  						InternalIP:    "192.168.0.13",
    40  						SSHPrivateKey: "id_rsa",
    41  						SSHPort:       2222,
    42  						SSHUser:       "alice",
    43  					},
    44  					{
    45  						Host:          "worker02",
    46  						PublicIP:      "10.0.0.4",
    47  						InternalIP:    "192.168.0.14",
    48  						SSHPrivateKey: "id_rsa",
    49  						SSHPort:       2222,
    50  						SSHUser:       "alice and bob",
    51  					},
    52  				},
    53  			},
    54  		},
    55  	}
    56  
    57  	ini := string(inv.ToINI())
    58  
    59  	expected := `[etcd]
    60  "etcd01" ansible_host="10.0.0.1" internal_ipv4="192.168.0.11" ansible_ssh_private_key_file="id_rsa" ansible_port=2222 ansible_user="alice"
    61  [master]
    62  "master01" ansible_host="10.0.0.2" internal_ipv4="192.168.0.12" ansible_ssh_private_key_file="id_rsa" ansible_port=2222 ansible_user="alice"
    63  [worker]
    64  "worker01" ansible_host="10.0.0.3" internal_ipv4="192.168.0.13" ansible_ssh_private_key_file="id_rsa" ansible_port=2222 ansible_user="alice"
    65  "worker02" ansible_host="10.0.0.4" internal_ipv4="192.168.0.14" ansible_ssh_private_key_file="id_rsa" ansible_port=2222 ansible_user="alice and bob"
    66  `
    67  
    68  	if ini != expected {
    69  		t.Errorf("expected format differs from obtained format. Expected: \n%s\nGot: \n%s\n", expected, ini)
    70  	}
    71  
    72  }