github.com/caos/orbos@v1.5.14-0.20221103111702-e6cd0cea7ad4/internal/operator/orbiter/kinds/providers/static/desired_test.go (about)

     1  package static
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  func TestMachine_validate(t *testing.T) {
     8  	type fields struct {
     9  		ID                  string
    10  		Hostname            string
    11  		IP                  string
    12  		RebootRequired      bool
    13  		ReplacementRequired bool
    14  	}
    15  	tests := []struct {
    16  		name    string
    17  		fields  fields
    18  		wantErr bool
    19  	}{{
    20  		name: "It should succeed when names are RFC 1123 compatible",
    21  		fields: fields{
    22  			ID:       "valid-name-1",
    23  			Hostname: "valid-name-1",
    24  			IP:       "127.0.0.1",
    25  		},
    26  		wantErr: false,
    27  	}, {
    28  		name: "It should fail when names have uppercase letters",
    29  		fields: fields{
    30  			ID:       "inValidName",
    31  			Hostname: "inValidName",
    32  			IP:       "127.0.0.1",
    33  		},
    34  		wantErr: true,
    35  	}, {
    36  		name: "It should fail when the ID is empty",
    37  		fields: fields{
    38  			Hostname: "valid-name-1",
    39  			IP:       "127.0.0.1",
    40  		},
    41  		wantErr: true,
    42  	}, {
    43  		name: "It should fail when the ID is too long",
    44  		fields: fields{
    45  			ID:       "a-too-long-name-that-possibly-breaks-many-applications-as-it-doesnt-adhere-to-rfc-1123",
    46  			Hostname: "valid-name-1",
    47  			IP:       "127.0.0.1",
    48  		},
    49  		wantErr: true,
    50  	}}
    51  	for _, tt := range tests {
    52  		t.Run(tt.name, func(t *testing.T) {
    53  			c := &Machine{
    54  				ID:                  tt.fields.ID,
    55  				Hostname:            tt.fields.Hostname,
    56  				IP:                  tt.fields.IP,
    57  				RebootRequired:      tt.fields.RebootRequired,
    58  				ReplacementRequired: tt.fields.ReplacementRequired,
    59  			}
    60  			if err := c.validate(); (err != nil) != tt.wantErr {
    61  				t.Errorf("validate() error = %v, wantErr %v", err, tt.wantErr)
    62  			}
    63  		})
    64  	}
    65  }