-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: make bootStrapURL as array and refactor code
Signed-off-by: Bhoopesh <[email protected]>
- Loading branch information
1 parent
facc163
commit 9af1b66
Showing
22 changed files
with
302 additions
and
169 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
SPDX-License-Identifier: Apache-2.0 | ||
Copyright (C) 2022-2023 Intel Corporation | ||
Copyright (c) 2022 Dell Inc, or its subsidiaries. | ||
Copyright (C) 2022 Red Hat. | ||
*/ | ||
|
||
// Package dhcp implements the DHCP client | ||
package dhcp | ||
|
||
import "log" | ||
|
||
// GetBootstrapURL returns the bootstrap URL | ||
func GetBootstrapURL(dhcpLeaseFile string) ([]string, error) { | ||
url, err := getBootstrapURLViaLeaseFile(dhcpLeaseFile) | ||
if err == nil { | ||
return []string{url}, nil | ||
} | ||
log.Println("[INFO] Trying to get the URL from NetworkManager") | ||
urls, err := getBootstrapURLViaNetworkManager() | ||
if err == nil { | ||
return urls, nil | ||
} | ||
return nil, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/* | ||
SPDX-License-Identifier: Apache-2.0 | ||
Copyright (C) 2022-2023 Intel Corporation | ||
Copyright (c) 2022 Dell Inc, or its subsidiaries. | ||
Copyright (C) 2022 Red Hat. | ||
*/ | ||
|
||
// Package dhcp implements the DHCP client | ||
package dhcp | ||
|
||
import "testing" | ||
|
||
func TestGetBootstrapURL(_ *testing.T) { | ||
// TODO: Implement the test | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
SPDX-License-Identifier: Apache-2.0 | ||
Copyright (C) 2022-2023 Intel Corporation | ||
Copyright (c) 2022 Dell Inc, or its subsidiaries. | ||
Copyright (C) 2022 Red Hat. | ||
*/ | ||
|
||
// Package dhcp implements the DHCP client | ||
package dhcp | ||
|
||
import ( | ||
"errors" | ||
"log" | ||
"os" | ||
) | ||
|
||
const sztpRedirectUrls = "sztp-redirect-urls" | ||
|
||
// getBootstrapURLViaLeaseFile returns the sztp redirect URL via DHCP lease file | ||
func getBootstrapURLViaLeaseFile(dhcpLeaseFile string) (string, error) { | ||
var line string | ||
if _, err := os.Stat(dhcpLeaseFile); err == nil { | ||
for { | ||
line = LinesInFileContains(dhcpLeaseFile, sztpRedirectUrls) | ||
if line != "" { | ||
break | ||
} | ||
} | ||
return ExtractfromLine(line, `(?m)[^"]*`, 1), nil | ||
} | ||
log.Println("[Error] File " + dhcpLeaseFile + " does not exist") | ||
return "", errors.New("File " + dhcpLeaseFile + " does not exist") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
SPDX-License-Identifier: Apache-2.0 | ||
Copyright (C) 2022-2023 Intel Corporation | ||
Copyright (c) 2022 Dell Inc, or its subsidiaries. | ||
Copyright (C) 2022 Red Hat. | ||
*/ | ||
|
||
// Package dhcp implements the DHCP client | ||
package dhcp | ||
|
||
import "testing" | ||
|
||
func TestGetBootstrapURLViaLeaseFile(t *testing.T) { | ||
dhcpTestFileOK := "/tmp/test.dhcp" | ||
CreateTempTestFile(dhcpTestFileOK, DHCPTestContent, true) | ||
|
||
type fields struct { | ||
DhcpLeaseFile string | ||
} | ||
tests := []struct { | ||
name string | ||
fields fields | ||
want string | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "Test OK Case file exists and get the URL", | ||
fields: fields{ | ||
DhcpLeaseFile: dhcpTestFileOK, | ||
}, | ||
want: "http://mymock/test", | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "Test KO Case file does not exist", | ||
fields: fields{ | ||
DhcpLeaseFile: "/kk/kk", | ||
}, | ||
want: "", | ||
wantErr: true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := getBootstrapURLViaLeaseFile(tt.fields.DhcpLeaseFile) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("GetBootstrapURLViaLeaseFile() error = %v, wantErr %v", err, tt.wantErr) | ||
} else if got != tt.want { | ||
t.Errorf("GetBootstrapURLViaLeaseFile() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.