github.com/renbou/grpcbridge@v0.0.2-0.20240416012907-bcbd8b12648a/internal/config/hcl_test.go (about) 1 package config 2 3 import ( 4 "path/filepath" 5 "testing" 6 7 "github.com/google/go-cmp/cmp" 8 ) 9 10 func testpath(filename string) string { 11 return filepath.Join("testdata", filename) 12 } 13 14 func Test_ReadHCL(t *testing.T) { 15 t.Parallel() 16 17 tests := []struct { 18 filename string 19 wantConfig *Bridge 20 }{ 21 { 22 filename: "config-single.hcl", 23 wantConfig: &Bridge{ 24 Services: []Service{{ 25 Name: "test", 26 Target: "127.0.0.1:50051", 27 }}, 28 }, 29 }, 30 { 31 filename: "config-multiple.hcl", 32 wantConfig: &Bridge{ 33 Services: []Service{ 34 { 35 Name: "testsvc1", 36 Target: "127.0.0.1:50052", 37 }, 38 { 39 Name: "testsvc2", 40 Target: "scheme://testsvc2:grpc", 41 }, 42 { 43 Name: "testsvc3", 44 Target: "https://127.0.0.1:50054", 45 }, 46 }, 47 }, 48 }, 49 { 50 filename: "config-json.json", 51 wantConfig: &Bridge{ 52 Services: []Service{ 53 { 54 Name: "testsvc", 55 Target: "localhost:50051", 56 }, 57 { 58 Name: "anothersvc", 59 Target: "localhost:50052", 60 }, 61 }, 62 }, 63 }, 64 } 65 66 for _, tt := range tests { 67 t.Run(tt.filename, func(t *testing.T) { 68 t.Parallel() 69 70 gotConfig, err := ReadHCL(testpath(tt.filename)) 71 if err != nil { 72 t.Fatalf("ReadHCL(%q) returned error = %v, want nil", tt.filename, err) 73 } 74 75 if diff := cmp.Diff(tt.wantConfig, gotConfig); diff != "" { 76 t.Errorf("ReadHCL(%q) returned config differing from expected (-want+got)\n%s", tt.filename, diff) 77 } 78 }) 79 } 80 } 81 82 func Test_ReadHCL_Error(t *testing.T) { 83 t.Parallel() 84 85 tests := []struct { 86 filename string 87 }{ 88 { 89 filename: "config-nonexistent.hcl", 90 }, 91 { 92 filename: "config-invalid.hcl", 93 }, 94 } 95 96 for _, tt := range tests { 97 t.Run(tt.filename, func(t *testing.T) { 98 t.Parallel() 99 100 if _, err := ReadHCL(testpath(tt.filename)); err == nil { 101 t.Errorf("ReadHCL(%q) returned nil error, want non-nil error for invalid configs", tt.filename) 102 } 103 }) 104 } 105 }