|
5 | 5 | "fmt" |
6 | 6 | "io/ioutil" |
7 | 7 | "path" |
| 8 | + "path/filepath" |
8 | 9 | "testing" |
9 | 10 |
|
10 | 11 | "github.com/stolostron/policy-generator-plugin/internal/types" |
@@ -582,7 +583,7 @@ policies: |
582 | 583 | } |
583 | 584 |
|
584 | 585 | expected := "may not use a mix of Placement and PlacementRule for " + |
585 | | - "policies; found 1 Placement and 1 PlacementRule" |
| 586 | + "policies and policysets; found 1 Placement and 1 PlacementRule" |
586 | 587 | assertEqual(t, err.Error(), expected) |
587 | 588 | } |
588 | 589 |
|
@@ -766,3 +767,215 @@ policies: |
766 | 767 | expected := fmt.Sprintf("could not read the placement rule path %s", plrPath) |
767 | 768 | assertEqual(t, err.Error(), expected) |
768 | 769 | } |
| 770 | + |
| 771 | +func TestPolicySetConfig(t *testing.T) { |
| 772 | + t.Parallel() |
| 773 | + tmpDir := t.TempDir() |
| 774 | + createConfigMap(t, tmpDir, "configmap.yaml") |
| 775 | + |
| 776 | + testCases := []testCase{ |
| 777 | + { |
| 778 | + name: "policySet must have a name set", |
| 779 | + setupFunc: func(p *Plugin) { |
| 780 | + p.PolicySets = []types.PolicySetConfig{ |
| 781 | + { |
| 782 | + Placement: types.PlacementConfig{ |
| 783 | + Name: "policyset-placement", |
| 784 | + ClusterSelectors: map[string]string{"my": "app"}, |
| 785 | + }, |
| 786 | + }, |
| 787 | + } |
| 788 | + }, |
| 789 | + expectedErrMsg: "each policySet must have a name set, but did not find a name at policySet array index 0", |
| 790 | + }, |
| 791 | + { |
| 792 | + name: "policySet must be unique", |
| 793 | + setupFunc: func(p *Plugin) { |
| 794 | + p.PolicySets = []types.PolicySetConfig{ |
| 795 | + { |
| 796 | + Name: "my-policyset", |
| 797 | + }, |
| 798 | + { |
| 799 | + Name: "my-policyset", |
| 800 | + }, |
| 801 | + } |
| 802 | + }, |
| 803 | + expectedErrMsg: "each policySet must have a unique name set, but found a duplicate name: my-policyset", |
| 804 | + }, |
| 805 | + { |
| 806 | + name: "policySet must provide only one of placementRulePath or placementPath", |
| 807 | + setupFunc: func(p *Plugin) { |
| 808 | + p.PolicySets = []types.PolicySetConfig{ |
| 809 | + { |
| 810 | + Name: "my-policyset", |
| 811 | + Placement: types.PlacementConfig{ |
| 812 | + PlacementPath: "../config/plc.yaml", |
| 813 | + PlacementRulePath: "../config/plr.yaml", |
| 814 | + }, |
| 815 | + }, |
| 816 | + } |
| 817 | + }, |
| 818 | + expectedErrMsg: "policySet my-policyset must provide only one of placementRulePath or placementPath", |
| 819 | + }, |
| 820 | + { |
| 821 | + name: "policySet must provide only one of labelSelector or clusterselectors", |
| 822 | + setupFunc: func(p *Plugin) { |
| 823 | + p.PolicySets = []types.PolicySetConfig{ |
| 824 | + { |
| 825 | + Name: "my-policyset", |
| 826 | + Placement: types.PlacementConfig{ |
| 827 | + LabelSelector: map[string]string{"cloud": "red hat"}, |
| 828 | + ClusterSelectors: map[string]string{"cloud": "red hat"}, |
| 829 | + }, |
| 830 | + }, |
| 831 | + } |
| 832 | + }, |
| 833 | + expectedErrMsg: "policySet my-policyset must provide only one of placement.labelSelector or placement.clusterselectors", |
| 834 | + }, |
| 835 | + { |
| 836 | + name: "policySet may not specify a cluster selector and placement path together", |
| 837 | + setupFunc: func(p *Plugin) { |
| 838 | + p.PolicySets = []types.PolicySetConfig{ |
| 839 | + { |
| 840 | + Name: "my-policyset", |
| 841 | + Placement: types.PlacementConfig{ |
| 842 | + PlacementPath: "../config/plc.yaml", |
| 843 | + ClusterSelectors: map[string]string{"cloud": "red hat"}, |
| 844 | + }, |
| 845 | + }, |
| 846 | + } |
| 847 | + }, |
| 848 | + expectedErrMsg: "policySet my-policyset may not specify a placement selector and placement path together", |
| 849 | + }, |
| 850 | + { |
| 851 | + name: "policySet may not specify a label selector and placement path together", |
| 852 | + setupFunc: func(p *Plugin) { |
| 853 | + p.PolicySets = []types.PolicySetConfig{ |
| 854 | + { |
| 855 | + Name: "my-policyset", |
| 856 | + Placement: types.PlacementConfig{ |
| 857 | + PlacementPath: "../config/plc.yaml", |
| 858 | + LabelSelector: map[string]string{"cloud": "red hat"}, |
| 859 | + }, |
| 860 | + }, |
| 861 | + } |
| 862 | + }, |
| 863 | + expectedErrMsg: "policySet my-policyset may not specify a placement selector and placement path together", |
| 864 | + }, |
| 865 | + { |
| 866 | + name: "policySet may not specify a cluster selector and placementrule path together", |
| 867 | + setupFunc: func(p *Plugin) { |
| 868 | + p.PolicySets = []types.PolicySetConfig{ |
| 869 | + { |
| 870 | + Name: "my-policyset", |
| 871 | + Placement: types.PlacementConfig{ |
| 872 | + PlacementRulePath: "../config/plc.yaml", |
| 873 | + ClusterSelectors: map[string]string{"cloud": "red hat"}, |
| 874 | + }, |
| 875 | + }, |
| 876 | + } |
| 877 | + }, |
| 878 | + expectedErrMsg: "policySet my-policyset may not specify a placement selector and placement path together", |
| 879 | + }, |
| 880 | + { |
| 881 | + name: "policySet may not specify a label selector and placementrule path together", |
| 882 | + setupFunc: func(p *Plugin) { |
| 883 | + p.PolicySets = []types.PolicySetConfig{ |
| 884 | + { |
| 885 | + Name: "my-policyset", |
| 886 | + Placement: types.PlacementConfig{ |
| 887 | + PlacementRulePath: "../config/plc.yaml", |
| 888 | + LabelSelector: map[string]string{"cloud": "red hat"}, |
| 889 | + }, |
| 890 | + }, |
| 891 | + } |
| 892 | + }, |
| 893 | + expectedErrMsg: "policySet my-policyset may not specify a placement selector and placement path together", |
| 894 | + }, |
| 895 | + { |
| 896 | + name: "policySet placementrule path not resolvable", |
| 897 | + setupFunc: func(p *Plugin) { |
| 898 | + p.PolicySets = []types.PolicySetConfig{ |
| 899 | + { |
| 900 | + Name: "my-policyset", |
| 901 | + Placement: types.PlacementConfig{ |
| 902 | + PlacementRulePath: "../config/plc.yaml", |
| 903 | + }, |
| 904 | + }, |
| 905 | + } |
| 906 | + }, |
| 907 | + expectedErrMsg: "could not read the placement rule path ../config/plc.yaml", |
| 908 | + }, |
| 909 | + { |
| 910 | + name: "policySet placement path not resolvable", |
| 911 | + setupFunc: func(p *Plugin) { |
| 912 | + p.PolicySets = []types.PolicySetConfig{ |
| 913 | + { |
| 914 | + Name: "my-policyset", |
| 915 | + Placement: types.PlacementConfig{ |
| 916 | + PlacementPath: "../config/plc.yaml", |
| 917 | + }, |
| 918 | + }, |
| 919 | + } |
| 920 | + }, |
| 921 | + expectedErrMsg: "could not read the placement path ../config/plc.yaml", |
| 922 | + }, |
| 923 | + { |
| 924 | + name: "Placement and PlacementRule can't be mixed", |
| 925 | + setupFunc: func(p *Plugin) { |
| 926 | + p.Policies[0].Placement = types.PlacementConfig{ |
| 927 | + LabelSelector: map[string]string{"cloud": "red hat"}, |
| 928 | + } |
| 929 | + p.PolicySets = []types.PolicySetConfig{ |
| 930 | + { |
| 931 | + Name: "my-policyset", |
| 932 | + Placement: types.PlacementConfig{ |
| 933 | + ClusterSelectors: map[string]string{"cloud": "red hat"}, |
| 934 | + }, |
| 935 | + }, |
| 936 | + } |
| 937 | + }, |
| 938 | + expectedErrMsg: "may not use a mix of Placement and PlacementRule for policies and policysets; found 1 Placement and 1 PlacementRule", |
| 939 | + }, |
| 940 | + } |
| 941 | + |
| 942 | + for _, tc := range testCases { |
| 943 | + tc := tc // capture range variable |
| 944 | + t.Run(tc.name, func(t *testing.T) { |
| 945 | + t.Parallel() |
| 946 | + p := Plugin{} |
| 947 | + var err error |
| 948 | + p.baseDirectory, err = filepath.EvalSymlinks(tmpDir) |
| 949 | + if err != nil { |
| 950 | + t.Fatal(err.Error()) |
| 951 | + } |
| 952 | + p.PlacementBindingDefaults.Name = "my-placement-binding" |
| 953 | + p.PolicyDefaults.Placement.Name = "my-placement-rule" |
| 954 | + p.PolicyDefaults.Namespace = "my-policies" |
| 955 | + policyConf1 := types.PolicyConfig{ |
| 956 | + Name: "policy-app-config", |
| 957 | + Manifests: []types.Manifest{ |
| 958 | + { |
| 959 | + Path: path.Join(tmpDir, "configmap.yaml"), |
| 960 | + }, |
| 961 | + }, |
| 962 | + } |
| 963 | + policyConf2 := types.PolicyConfig{ |
| 964 | + Name: "policy-app-config2", |
| 965 | + Manifests: []types.Manifest{ |
| 966 | + { |
| 967 | + Path: path.Join(tmpDir, "configmap.yaml"), |
| 968 | + }, |
| 969 | + }, |
| 970 | + } |
| 971 | + p.Policies = append(p.Policies, policyConf1, policyConf2) |
| 972 | + tc.setupFunc(&p) |
| 973 | + p.applyDefaults(map[string]interface{}{}) |
| 974 | + err = p.assertValidConfig() |
| 975 | + if err == nil { |
| 976 | + t.Fatal("Expected an error but did not get one") |
| 977 | + } |
| 978 | + assertEqual(t, err.Error(), tc.expectedErrMsg) |
| 979 | + }) |
| 980 | + } |
| 981 | +} |
0 commit comments