github.com/leowmjw/otto@v0.2.1-0.20160126165905-6400716cf085/appfile/file_test.go (about)

     1  package appfile
     2  
     3  import (
     4  	"path/filepath"
     5  	"reflect"
     6  	"testing"
     7  
     8  	"github.com/mitchellh/copystructure"
     9  )
    10  
    11  func TestFileActiveInfrastructure(t *testing.T) {
    12  	cases := []struct {
    13  		File   string
    14  		Result string
    15  	}{
    16  		{
    17  			"file-active-infra-basic.hcl",
    18  			"aws",
    19  		},
    20  	}
    21  
    22  	for _, tc := range cases {
    23  		path := filepath.Join("./test-fixtures", tc.File)
    24  		actual, err := ParseFile(path)
    25  		if err != nil {
    26  			t.Fatalf("file: %s\n\n%s", tc.File, err)
    27  			continue
    28  		}
    29  
    30  		infra := actual.ActiveInfrastructure()
    31  		if infra.Name != tc.Result {
    32  			t.Fatalf("file: %s\n\n%s", tc.File, infra.Name)
    33  		}
    34  	}
    35  }
    36  
    37  func TestFileMerge(t *testing.T) {
    38  	cases := map[string]struct {
    39  		One, Two, Three *File
    40  	}{
    41  		"ID": {
    42  			One: &File{
    43  				ID: "foo",
    44  			},
    45  			Two: &File{
    46  				ID: "bar",
    47  			},
    48  			Three: &File{
    49  				ID: "bar",
    50  			},
    51  		},
    52  
    53  		"Path": {
    54  			One: &File{
    55  				Path: "foo",
    56  			},
    57  			Two: &File{
    58  				Path: "bar",
    59  			},
    60  			Three: &File{
    61  				Path: "bar",
    62  			},
    63  		},
    64  
    65  		"Application": {
    66  			One: &File{
    67  				Application: &Application{
    68  					Name: "foo",
    69  				},
    70  			},
    71  			Two: &File{
    72  				Application: &Application{
    73  					Type: "foo",
    74  				},
    75  			},
    76  			Three: &File{
    77  				Application: &Application{
    78  					Name: "foo",
    79  					Type: "foo",
    80  				},
    81  			},
    82  		},
    83  
    84  		"Application (no merge)": {
    85  			One: &File{
    86  				Application: &Application{
    87  					Name: "foo",
    88  				},
    89  			},
    90  			Two: &File{},
    91  			Three: &File{
    92  				Application: &Application{
    93  					Name: "foo",
    94  				},
    95  			},
    96  		},
    97  
    98  		"Application (detect)": {
    99  			One: &File{
   100  				Application: &Application{
   101  					Name:   "foo",
   102  					Detect: true,
   103  				},
   104  			},
   105  			Two: &File{
   106  				Application: &Application{
   107  					Type:   "foo",
   108  					Detect: false,
   109  				},
   110  			},
   111  			Three: &File{
   112  				Application: &Application{
   113  					Name:   "foo",
   114  					Type:   "foo",
   115  					Detect: false,
   116  				},
   117  			},
   118  		},
   119  
   120  		"Infra (no merge)": {
   121  			One: &File{
   122  				Infrastructure: []*Infrastructure{
   123  					&Infrastructure{
   124  						Name: "aws",
   125  					},
   126  				},
   127  			},
   128  			Two: &File{},
   129  			Three: &File{
   130  				Infrastructure: []*Infrastructure{
   131  					&Infrastructure{
   132  						Name: "aws",
   133  					},
   134  				},
   135  			},
   136  		},
   137  
   138  		"Infra (add)": {
   139  			One: &File{
   140  				Infrastructure: []*Infrastructure{
   141  					&Infrastructure{
   142  						Name: "aws",
   143  					},
   144  				},
   145  			},
   146  			Two: &File{
   147  				Infrastructure: []*Infrastructure{
   148  					&Infrastructure{
   149  						Name: "google",
   150  					},
   151  				},
   152  			},
   153  			Three: &File{
   154  				Infrastructure: []*Infrastructure{
   155  					&Infrastructure{
   156  						Name: "aws",
   157  					},
   158  					&Infrastructure{
   159  						Name: "google",
   160  					},
   161  				},
   162  			},
   163  		},
   164  
   165  		"Infra (override)": {
   166  			One: &File{
   167  				Infrastructure: []*Infrastructure{
   168  					&Infrastructure{
   169  						Name: "aws",
   170  					},
   171  				},
   172  			},
   173  			Two: &File{
   174  				Infrastructure: []*Infrastructure{
   175  					&Infrastructure{
   176  						Name: "aws",
   177  					},
   178  				},
   179  			},
   180  			Three: &File{
   181  				Infrastructure: []*Infrastructure{
   182  					&Infrastructure{
   183  						Name: "aws",
   184  					},
   185  				},
   186  			},
   187  		},
   188  
   189  		"Foundations (none)": {
   190  			One: &File{
   191  				Infrastructure: []*Infrastructure{
   192  					&Infrastructure{
   193  						Name: "aws",
   194  						Foundations: []*Foundation{
   195  							&Foundation{
   196  								Name: "consul",
   197  							},
   198  						},
   199  					},
   200  				},
   201  			},
   202  			Two: &File{
   203  				Infrastructure: []*Infrastructure{
   204  					&Infrastructure{
   205  						Name: "aws",
   206  					},
   207  				},
   208  			},
   209  			Three: &File{
   210  				Infrastructure: []*Infrastructure{
   211  					&Infrastructure{
   212  						Name: "aws",
   213  						Foundations: []*Foundation{
   214  							&Foundation{
   215  								Name: "consul",
   216  							},
   217  						},
   218  					},
   219  				},
   220  			},
   221  		},
   222  
   223  		"Foundations (override)": {
   224  			One: &File{
   225  				Infrastructure: []*Infrastructure{
   226  					&Infrastructure{
   227  						Name: "aws",
   228  						Foundations: []*Foundation{
   229  							&Foundation{
   230  								Name: "consul",
   231  							},
   232  						},
   233  					},
   234  				},
   235  			},
   236  			Two: &File{
   237  				Infrastructure: []*Infrastructure{
   238  					&Infrastructure{
   239  						Name: "aws",
   240  						Foundations: []*Foundation{
   241  							&Foundation{
   242  								Name: "tubes",
   243  							},
   244  						},
   245  					},
   246  				},
   247  			},
   248  			Three: &File{
   249  				Infrastructure: []*Infrastructure{
   250  					&Infrastructure{
   251  						Name: "aws",
   252  						Foundations: []*Foundation{
   253  							&Foundation{
   254  								Name: "tubes",
   255  							},
   256  						},
   257  					},
   258  				},
   259  			},
   260  		},
   261  	}
   262  
   263  	for name, tc := range cases {
   264  		if err := tc.One.Merge(tc.Two); err != nil {
   265  			t.Fatalf("%s: %s", name, err)
   266  		}
   267  
   268  		if !reflect.DeepEqual(tc.One, tc.Three) {
   269  			t.Fatalf("%s:\n\n%#v\n\n%#v", name, tc.One, tc.Three)
   270  		}
   271  	}
   272  }
   273  
   274  func TestFileDeepCopy(t *testing.T) {
   275  	f, err := ParseFile(filepath.Join("./test-fixtures", "basic.hcl"))
   276  	if err != nil {
   277  		t.Fatalf("err: %s", err)
   278  	}
   279  
   280  	f2, err := copystructure.Copy(f)
   281  	if err != nil {
   282  		t.Fatalf("err: %s", err)
   283  	}
   284  
   285  	if !reflect.DeepEqual(f, f2) {
   286  		t.Fatalf("bad:\n\n%#v\n\n%#v", f, f2)
   287  	}
   288  }