github.com/avenga/couper@v1.12.2/config/configload/deprecated_test.go (about)

     1  package configload
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/google/go-cmp/cmp"
     8  	"github.com/hashicorp/hcl/v2/hclsyntax"
     9  
    10  	"github.com/avenga/couper/config/parser"
    11  	"github.com/avenga/couper/internal/test"
    12  )
    13  
    14  func Test_deprecated(t *testing.T) {
    15  	// Insert test data:
    16  	deprecatedAttributes["couper_test_attribute"] = deprecated{"couper_new_attribute", "1.23"}
    17  	deprecatedBlocks["couper_test_block"] = deprecated{"couper_new_block", "1.23"}
    18  	deprecatedLabels["couper_test_label"] = deprecated{"couper_new_label", "1.23"}
    19  
    20  	src := []byte(`
    21  error_handler "x" "couper_test_label" "abc couper_test_label def" "y" {
    22  	couper_test_attribute = true
    23  	couper_test_block {
    24  	}
    25  }
    26  `)
    27  
    28  	body, err := parser.Load(src, "test.go")
    29  	if err != nil {
    30  		t.Fatalf("%s", err)
    31  	}
    32  
    33  	logger, hook := test.NewLogger()
    34  	hook.Reset()
    35  
    36  	deprecate([]*hclsyntax.Body{body}, logger.WithContext(context.TODO()))
    37  
    38  	if len(body.Blocks) != 1 {
    39  		t.Fatal("Unexpected number of blocks given")
    40  	}
    41  
    42  	if len(body.Blocks[0].Body.Attributes) != 1 {
    43  		t.Fatal("Unexpected number of attributes given")
    44  	}
    45  
    46  	if attr, exists := body.Blocks[0].Body.Attributes["couper_new_attribute"]; !exists {
    47  		t.Error("Missing 'couper_new_attribute' attribute")
    48  	} else if attr.Name != "couper_test_attribute" {
    49  		t.Errorf("Unexpected attribute name given: '%s'", attr.Name)
    50  	}
    51  
    52  	if body.Blocks[0].Body.Blocks[0].Type != "couper_new_block" {
    53  		t.Errorf("Expected 'couper_new_block' block name, got '%s'", body.Blocks[0].Type)
    54  	}
    55  
    56  	expLabels := []string{"x", "couper_new_label", "abc", "couper_new_label", "def", "y"}
    57  	if !cmp.Equal(expLabels, body.Blocks[0].Labels) {
    58  		t.Errorf("Expected\n%#v, got:\n%#v", expLabels, body.Blocks[0].Labels)
    59  	}
    60  
    61  	entries := hook.AllEntries()
    62  	if len(entries) != 4 {
    63  		t.Fatal("Unexpected number of log entries given")
    64  	}
    65  
    66  	exp := `replacing label "couper_test_label" with "couper_new_label"; as of Couper version 1.23, the old value is no longer supported`
    67  	if entries[0].Message != exp {
    68  		t.Errorf("Expected\n%#v, got:\n%#v", exp, entries[0].Message)
    69  	}
    70  	if entries[1].Message != exp {
    71  		t.Errorf("Expected\n%#v, got:\n%#v", exp, entries[0].Message)
    72  	}
    73  
    74  	exp = `replacing attribute "couper_test_attribute" with "couper_new_attribute"; as of Couper version 1.23, the old value is no longer supported`
    75  	if entries[2].Message != exp {
    76  		t.Errorf("Expected\n%#v, got:\n%#v", exp, entries[0].Message)
    77  	}
    78  
    79  	exp = `replacing block "couper_test_block" with "couper_new_block"; as of Couper version 1.23, the old value is no longer supported`
    80  	if entries[3].Message != exp {
    81  		t.Errorf("Expected\n%#v, got:\n%#v", exp, entries[0].Message)
    82  	}
    83  }