|
| 1 | +// Copyright 2020 Microsoft. All rights reserved. |
| 2 | +// MIT License |
| 3 | + |
| 4 | +package restserver |
| 5 | + |
| 6 | +import ( |
| 7 | + "strconv" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/Azure/azure-container-networking/cns" |
| 11 | + "github.com/Azure/azure-container-networking/cns/fakes" |
| 12 | + "github.com/Azure/azure-container-networking/cns/types" |
| 13 | + "github.com/Azure/azure-container-networking/iptables" |
| 14 | + "github.com/Azure/azure-container-networking/network/networkutils" |
| 15 | +) |
| 16 | + |
| 17 | +type FakeIPTablesProvider struct { |
| 18 | + iptables *fakes.IPTablesMock |
| 19 | +} |
| 20 | + |
| 21 | +func (c *FakeIPTablesProvider) GetIPTables() (iptablesClient, error) { |
| 22 | + // persist iptables in testing |
| 23 | + if c.iptables == nil { |
| 24 | + c.iptables = fakes.NewIPTablesMock() |
| 25 | + } |
| 26 | + return c.iptables, nil |
| 27 | +} |
| 28 | + |
| 29 | +func TestAddSNATRules(t *testing.T) { |
| 30 | + type expectedScenario struct { |
| 31 | + table string |
| 32 | + chain string |
| 33 | + rule []string |
| 34 | + } |
| 35 | + |
| 36 | + tests := []struct { |
| 37 | + name string |
| 38 | + input *cns.CreateNetworkContainerRequest |
| 39 | + expected []expectedScenario |
| 40 | + }{ |
| 41 | + { |
| 42 | + // in pod subnet, the primary nic ip is in the same address space as the pod subnet |
| 43 | + name: "podsubnet", |
| 44 | + input: &cns.CreateNetworkContainerRequest{ |
| 45 | + NetworkContainerid: ncID, |
| 46 | + IPConfiguration: cns.IPConfiguration{ |
| 47 | + IPSubnet: cns.IPSubnet{ |
| 48 | + IPAddress: "240.1.2.1", |
| 49 | + PrefixLength: 24, |
| 50 | + }, |
| 51 | + }, |
| 52 | + SecondaryIPConfigs: map[string]cns.SecondaryIPConfig{ |
| 53 | + "abc": { |
| 54 | + IPAddress: "240.1.2.7", |
| 55 | + }, |
| 56 | + }, |
| 57 | + HostPrimaryIP: "10.0.0.4", |
| 58 | + }, |
| 59 | + expected: []expectedScenario{ |
| 60 | + { |
| 61 | + table: iptables.Nat, |
| 62 | + chain: SWIFT, |
| 63 | + rule: []string{ |
| 64 | + "-m", "addrtype", "!", "--dst-type", "local", "-s", "240.1.2.0/24", "-d", |
| 65 | + networkutils.AzureDNS, "-p", iptables.UDP, "--dport", strconv.Itoa(iptables.DNSPort), "-j", iptables.Snat, "--to", "240.1.2.1", |
| 66 | + }, |
| 67 | + }, |
| 68 | + { |
| 69 | + table: iptables.Nat, |
| 70 | + chain: SWIFT, |
| 71 | + rule: []string{ |
| 72 | + "-m", "addrtype", "!", "--dst-type", "local", "-s", "240.1.2.0/24", "-d", |
| 73 | + networkutils.AzureDNS, "-p", iptables.TCP, "--dport", strconv.Itoa(iptables.DNSPort), "-j", iptables.Snat, "--to", "240.1.2.1", |
| 74 | + }, |
| 75 | + }, |
| 76 | + { |
| 77 | + table: iptables.Nat, |
| 78 | + chain: SWIFT, |
| 79 | + rule: []string{ |
| 80 | + "-m", "addrtype", "!", "--dst-type", "local", "-s", "240.1.2.0/24", "-d", |
| 81 | + networkutils.AzureIMDS, "-p", iptables.TCP, "--dport", strconv.Itoa(iptables.HTTPPort), "-j", iptables.Snat, "--to", "10.0.0.4", |
| 82 | + }, |
| 83 | + }, |
| 84 | + }, |
| 85 | + }, |
| 86 | + { |
| 87 | + // in vnet scale, the primary nic ip becomes the node ip (diff address space from pod subnet) |
| 88 | + name: "vnet scale", |
| 89 | + input: &cns.CreateNetworkContainerRequest{ |
| 90 | + NetworkContainerid: ncID, |
| 91 | + IPConfiguration: cns.IPConfiguration{ |
| 92 | + IPSubnet: cns.IPSubnet{ |
| 93 | + IPAddress: "10.0.0.4", |
| 94 | + PrefixLength: 28, |
| 95 | + }, |
| 96 | + }, |
| 97 | + SecondaryIPConfigs: map[string]cns.SecondaryIPConfig{ |
| 98 | + "abc": { |
| 99 | + IPAddress: "240.1.2.15", |
| 100 | + }, |
| 101 | + }, |
| 102 | + HostPrimaryIP: "10.0.0.4", |
| 103 | + }, |
| 104 | + expected: []expectedScenario{ |
| 105 | + { |
| 106 | + table: iptables.Nat, |
| 107 | + chain: SWIFT, |
| 108 | + rule: []string{ |
| 109 | + "-m", "addrtype", "!", "--dst-type", "local", "-s", "240.1.2.0/28", "-d", |
| 110 | + networkutils.AzureDNS, "-p", iptables.UDP, "--dport", strconv.Itoa(iptables.DNSPort), "-j", iptables.Snat, "--to", "10.0.0.4", |
| 111 | + }, |
| 112 | + }, |
| 113 | + { |
| 114 | + table: iptables.Nat, |
| 115 | + chain: SWIFT, |
| 116 | + rule: []string{ |
| 117 | + "-m", "addrtype", "!", "--dst-type", "local", "-s", "240.1.2.0/28", "-d", |
| 118 | + networkutils.AzureDNS, "-p", iptables.TCP, "--dport", strconv.Itoa(iptables.DNSPort), "-j", iptables.Snat, "--to", "10.0.0.4", |
| 119 | + }, |
| 120 | + }, |
| 121 | + { |
| 122 | + table: iptables.Nat, |
| 123 | + chain: SWIFT, |
| 124 | + rule: []string{ |
| 125 | + "-m", "addrtype", "!", "--dst-type", "local", "-s", "240.1.2.0/28", "-d", |
| 126 | + networkutils.AzureIMDS, "-p", iptables.TCP, "--dport", strconv.Itoa(iptables.HTTPPort), "-j", iptables.Snat, "--to", "10.0.0.4", |
| 127 | + }, |
| 128 | + }, |
| 129 | + }, |
| 130 | + }, |
| 131 | + } |
| 132 | + |
| 133 | + for _, tt := range tests { |
| 134 | + service := getTestService() |
| 135 | + service.iptables = &FakeIPTablesProvider{} |
| 136 | + resp, msg := service.programSNATRules(tt.input) |
| 137 | + if resp != types.Success { |
| 138 | + t.Fatal("failed to program snat rules", msg, " case: ", tt.name) |
| 139 | + } |
| 140 | + finalState, _ := service.iptables.GetIPTables() |
| 141 | + for _, ex := range tt.expected { |
| 142 | + exists, err := finalState.Exists(ex.table, ex.chain, ex.rule...) |
| 143 | + if err != nil || !exists { |
| 144 | + t.Fatal("rule not found", ex.rule, " case: ", tt.name) |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | +} |
0 commit comments