This is an automated email from the ASF dual-hosted git repository.

zrhoffman pushed a commit to branch 6.0.x
in repository https://gitbox.apache.org/repos/asf/trafficcontrol.git

commit 7b3a4a8e745edf6b9e0d85c977c8d90ef986deb7
Author: Robert O Butts <[email protected]>
AuthorDate: Mon Sep 13 10:13:04 2021 -0600

    Fix t3c-apply default hostname to be short (#6187)
    
    Fixes #6174
    
    (cherry picked from commit fee39e2b353a03356d6a2717b83e4e1e5383d821)
---
 CHANGELOG.md                                       |   1 +
 cache-config/t3c-apply/config/config.go            |   2 +
 cache-config/testing/docker/docker-compose.yml     |   1 +
 .../ort-tests/t3c-os-hostname-short_test.go        | 127 +++++++++++++++++++++
 4 files changed, 131 insertions(+)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index ed62ce2..af5057b 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -102,6 +102,7 @@ The format is based on [Keep a 
Changelog](http://keepachangelog.com/en/1.0.0/).
 - [#6066](https://github.com/apache/trafficcontrol/issues/6066) - Fixed 
missing/incorrect indices on some tables
 - [#6169](https://github.com/apache/trafficcontrol/issues/6169) - Fixed 
t3c-update not updating server status when a fallback to a previous Traffic Ops 
API version occurred
 - [#5576](https://github.com/apache/trafficcontrol/issues/5576) - Inconsistent 
Profile Name restrictions
+- [#6174](https://github.com/apache/trafficcontrol/issues/6174) - Fixed 
t3c-apply with no hostname failing if the OS hostname returns a full FQDN
 - Fixed Federations IMS so TR federations watcher will get updates.
 - [#5129](https://github.com/apache/trafficcontrol/issues/5129) - Updated TM 
so that it returns a 404 if the endpoint is not supported.
 
diff --git a/cache-config/t3c-apply/config/config.go 
b/cache-config/t3c-apply/config/config.go
index 6bfdcc9..ba33260 100644
--- a/cache-config/t3c-apply/config/config.go
+++ b/cache-config/t3c-apply/config/config.go
@@ -369,6 +369,8 @@ If any of the related flags are also set, they override the 
mode's default behav
                if err != nil {
                        return Cfg{}, errors.New("Could not get the hostname 
from the O.S., please supply a hostname: " + err.Error())
                }
+               // strings.Split always returns a slice with at least 1 
element, so we don't need a len check
+               cacheHostName = strings.Split(cacheHostName, ".")[0]
        }
 
        useGit := StrToUseGitFlag(*useGitStr)
diff --git a/cache-config/testing/docker/docker-compose.yml 
b/cache-config/testing/docker/docker-compose.yml
index f5d9215..cba6697 100644
--- a/cache-config/testing/docker/docker-compose.yml
+++ b/cache-config/testing/docker/docker-compose.yml
@@ -85,6 +85,7 @@ services:
         RHEL_VERSION: ${RHEL_VERSION:-8}
       context: .
       dockerfile: ort_test/Dockerfile
+    cap_add: ['SYS_ADMIN'] # necessary for hostname tests
     depends_on:
       - yumserver
       - to_server
diff --git a/cache-config/testing/ort-tests/t3c-os-hostname-short_test.go 
b/cache-config/testing/ort-tests/t3c-os-hostname-short_test.go
new file mode 100644
index 0000000..747e113
--- /dev/null
+++ b/cache-config/testing/ort-tests/t3c-os-hostname-short_test.go
@@ -0,0 +1,127 @@
+package orttest
+
+/*
+   Licensed under the Apache License, Version 2.0 (the "License");
+   you may not use this file except in compliance with the License.
+   You may obtain a copy of the License at
+
+   http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+*/
+
+import (
+       "strings"
+       "testing"
+
+       "github.com/apache/trafficcontrol/cache-config/t3cutil"
+       "github.com/apache/trafficcontrol/cache-config/testing/ort-tests/tcdata"
+)
+
+func TestT3cApplyOSHostnameShort(t *testing.T) {
+       t.Log("------------- Starting TestT3cApplyOSHostnameShort tests 
---------------")
+       tcd.WithObjs(t, []tcdata.TCObj{
+               tcdata.CDNs, tcdata.Types, tcdata.Tenants, tcdata.Parameters,
+               tcdata.Profiles, tcdata.ProfileParameters, tcdata.Statuses,
+               tcdata.Divisions, tcdata.Regions, tcdata.PhysLocations,
+               tcdata.CacheGroups, tcdata.Servers, tcdata.Topologies,
+               tcdata.DeliveryServices}, func() {
+               doTestT3cApplyOSHostnameShort(t)
+       })
+       t.Log("------------- End of TestT3cApplyOSHostnameShort tests 
---------------")
+}
+
+func doTestT3cApplyOSHostnameShort(t *testing.T) {
+       // verifies that when no hostname is passed,
+       // t3c-apply will get the OS hostname,
+       // and use the short name (which is what will be in Traffic Ops),
+       // and not the full FQDN.
+
+       startingHost, errCode := getHostName()
+       if errCode != 0 {
+               t.Fatalf("getting the hostname failed: code %v message %v", 
errCode, startingHost)
+       }
+
+       t.Logf("original host is " + startingHost)
+
+       cacheHostName := "atlanta-edge-03"
+
+       if msg, code := setHostName(cacheHostName); code != 0 {
+               t.Fatalf("setting the hostname failed: code %v message %v", 
code, msg)
+       }
+
+       defer func() {
+               if msg, code := setHostName(startingHost); code != 0 {
+                       t.Fatalf("setting the hostname back to the original 
'%v' failed: code %v message %v", startingHost, code, msg)
+               } else {
+                       t.Logf("set hostname back to original '" + startingHost 
+ "'")
+               }
+       }()
+
+       // verify the host was really set
+       if newHost, errCode := getHostName(); errCode != 0 {
+               t.Fatalf("getting the hostname failed: code %v message %v", 
errCode, newHost)
+       } else if newHost != cacheHostName {
+               t.Fatalf("setting hostname claimed it succeeded, but was '%v' 
expected '%v'", newHost, cacheHostName)
+       } else {
+               t.Logf("set hostname to '" + newHost + "'")
+       }
+
+       t.Logf("calling t3c-apply with no host flag, with a short hostname")
+       if stdOut, exitCode := t3cApplyNoHost(); exitCode != 0 {
+               t.Fatalf("t3c-apply with no hostname arg and and system 
hostname '%v' failed: code '%v' output '%v'\n", cacheHostName, exitCode, stdOut)
+       }
+
+       fqdnHostName := cacheHostName + ".fqdn.example.test"
+       if msg, code := setHostName(fqdnHostName); code != 0 {
+               t.Fatalf("setting the hostname failed: code %v message %v", 
code, msg)
+       }
+
+       // verify the host was really set
+       if newHost, errCode := getHostName(); errCode != 0 {
+               t.Fatalf("getting the hostname failed: code %v message %v", 
errCode, newHost)
+       } else if newHost != fqdnHostName {
+               t.Fatalf("setting hostname claimed it succeeded, but was '%v' 
expected '%v'", newHost, fqdnHostName)
+       } else {
+               t.Logf("set hostname to '" + newHost + "'")
+       }
+
+       t.Logf("calling t3c-apply with no host flag, with a fqdn hostname")
+       if stdOut, exitCode := t3cApplyNoHost(); exitCode != 0 {
+               t.Fatalf("t3c-apply with no hostname arg and and system 
hostname '%v' failed: code '%v' output '%v'\n", cacheHostName, exitCode, stdOut)
+       }
+}
+
+func setHostName(host string) (string, int) {
+       stdOut, stdErr, exitCode := t3cutil.Do("hostname", host)
+       return "out: " + string(stdOut) + " err: " + string(stdErr), exitCode
+}
+
+func getHostName() (string, int) {
+       stdOut, stdErr, exitCode := t3cutil.Do("hostname")
+       if exitCode == 0 {
+               return strings.TrimSpace(string(stdOut)), 0
+       }
+       return "out: " + string(stdOut) + " err: " + string(stdErr), exitCode
+}
+
+func t3cApplyNoHost() (string, int) {
+       args := []string{
+               "apply",
+               "--traffic-ops-insecure=true",
+               "--traffic-ops-timeout-milliseconds=3000",
+               "--traffic-ops-user=" + tcd.Config.TrafficOps.Users.Admin,
+               "--traffic-ops-password=" + tcd.Config.TrafficOps.UserPassword,
+               "--traffic-ops-url=" + tcd.Config.TrafficOps.URL,
+               "-vv",
+               "--omit-via-string-release=true",
+               "--git=" + "yes",
+               "--run-mode=" + "badass",
+       }
+       _, stdErr, exitCode := t3cutil.Do("t3c", args...)
+       return string(stdErr), exitCode
+}

Reply via email to