Merge pull request #2220 from andschwa/docker

Dockerize Linux build environments
This commit is contained in:
Sergei Vorobev authored and GitHub committed 2016-09-12 18:27:36 -07:00
commit a4b7db7277
16 files changed
+456 -103

No files matched your search

-7
View File
@@ -1,7 +0,0 @@
FROM ubuntu:16.04
RUN apt-get update && apt-get install -y curl libunwind8 libicu55 libcurl3
RUN curl -LO https://github.com/PowerShell/PowerShell/releases/download/v6.0.0-alpha.9/powershell_6.0.0-alpha.9-1ubuntu1.16.04.1_amd64.deb
RUN dpkg -i powershell_6.0.0-alpha.9-1ubuntu1.16.04.1_amd64.deb
Entrypoint [ "powershell" ]
+3 -3
View File
@@ -27,8 +27,8 @@ You can download and install a PowerShell package for any of the following platf
| Ubuntu 16.04 | [.deb][rl-ubuntu16] | [Instructions][in-ubuntu16] |
| Ubuntu 14.04 | [.deb][rl-ubuntu14] | [Instructions][in-ubuntu14] |
| CentOS 7 | [.rpm][rl-centos] | [Instructions][in-centos] |
| OS X 10.11 | [.pkg][rl-macos] | [Instructions][in-macos] |
| Docker | | [Instructions] [in-docker] |
| macOS 10.11 | [.pkg][rl-macos] | [Instructions][in-macos] |
| Docker | | [Instructions][in-docker] |
[rl-windows10]: https://github.com/PowerShell/PowerShell/releases/download/v6.0.0-alpha.9/PowerShell_6.0.0.9-alpha.9-win10-x64.msi
[rl-windows81]: https://github.com/PowerShell/PowerShell/releases/download/v6.0.0-alpha.9/PowerShell_6.0.0.9-alpha.9-win81-x64.msi
@@ -43,7 +43,7 @@ You can download and install a PowerShell package for any of the following platf
[in-ubuntu16]: docs/installation/linux.md#ubuntu-1604
[in-centos]: docs/installation/linux.md#centos-7
[in-macos]: docs/installation/linux.md#macos-1011
[in-docker]: docs/installation/docker.md
[in-docker]: docker
To install a specific version, visit [releases](https://github.com/PowerShell/PowerShell/releases).
+83 -52
View File
@@ -557,13 +557,14 @@ function Start-PSPester {
[string]$Path = "$PSScriptRoot/test/powershell",
[switch]$ThrowOnFailure,
[switch]$FullCLR,
[string]$binDir = (Split-Path (New-PSOptions -FullCLR:$FullCLR).Output)
[string]$binDir = (Split-Path (New-PSOptions -FullCLR:$FullCLR).Output),
[string]$powershell = (Join-Path $binDir 'powershell'),
[string]$Pester = ([IO.Path]::Combine($binDir, "Modules", "Pester"))
)
Write-Verbose "Running pester tests at '$path' with tag '$($Tag -join ''', ''')' and ExcludeTag '$($ExcludeTag -join ''', ''')'" -Verbose
# All concatenated commands/arguments are suffixed with the delimiter (space)
$Command = ""
$powershell = Join-Path $binDir 'powershell'
# Windows needs the execution policy adjusted
if ($IsWindows) {
@@ -571,10 +572,9 @@ function Start-PSPester {
}
$startParams = @{binDir=$binDir}
$PesterModule = [IO.Path]::Combine($binDir, "Modules", "Pester")
if(!$FullCLR)
{
$Command += "Import-Module '$PesterModule'; "
$Command += "Import-Module '$Pester'; "
}
$Command += "Invoke-Pester "
@@ -679,15 +679,70 @@ function Start-PSxUnit {
}
function Install-Dotnet {
[CmdletBinding()]
param(
[string]$Channel = "rel-1.0.0",
[string]$Version = "latest",
[switch]$NoSudo
)
# This allows sudo install to be optional; needed when running in containers / as root
# Note that when it is null, Invoke-Expression (but not &) must be used to interpolate properly
$sudo = if (!$NoSudo) { "sudo" }
$obtainUrl = "https://raw.githubusercontent.com/dotnet/cli/rel/1.0.0/scripts/obtain"
# Install for Linux and OS X
if ($IsLinux -or $IsOSX) {
# Uninstall all previous dotnet packages
$uninstallScript = if ($IsUbuntu) {
"dotnet-uninstall-debian-packages.sh"
} elseif ($IsOSX) {
"dotnet-uninstall-pkgs.sh"
}
if ($uninstallScript) {
Start-NativeExecution {
curl -sO $obtainUrl/uninstall/$uninstallScript
Invoke-Expression "$sudo bash ./$uninstallScript"
}
} else {
Write-Warning "This script only removes prior versions of dotnet for Ubuntu 14.04 and OS X"
}
# Install new dotnet 1.0.0 preview packages
$installScript = "dotnet-install.sh"
Start-NativeExecution {
curl -sO $obtainUrl/$installScript
bash ./$installScript -c $Channel -v $Version
}
# .NET Core's crypto library needs brew's OpenSSL libraries added to its rpath
if ($IsOSX) {
# This is the library shipped with .NET Core
# This is allowed to fail as the user may have installed other versions of dotnet
Write-Warning ".NET Core links the incorrect OpenSSL, correcting .NET CLI libraries..."
find $env:HOME/.dotnet -name System.Security.Cryptography.Native.dylib | xargs sudo install_name_tool -add_rpath /usr/local/opt/openssl/lib
}
} elseif ($IsWindows) {
Remove-Item -ErrorAction SilentlyContinue -Recurse -Force ~\AppData\Local\Microsoft\dotnet
$installScript = "dotnet-install.ps1"
Invoke-WebRequest -Uri $obtainUrl/$installScript -OutFile $installScript
& ./$installScript -c $Channel -v $Version
}
}
function Start-PSBootstrap {
[CmdletBinding(
SupportsShouldProcess=$true,
ConfirmImpact="High")]
param(
[ValidateSet("dev", "beta", "preview")]
[string]$Channel = "rel-1.0.0",
[string]$Version = "latest",
[switch]$Package,
[switch]$NoSudo,
[switch]$Force
)
@@ -695,6 +750,10 @@ function Start-PSBootstrap {
Push-Location $PSScriptRoot/tools
# This allows sudo install to be optional; needed when running in containers / as root
# Note that when it is null, Invoke-Expression (but not &) must be used to interpolate properly
$sudo = if (!$NoSudo) { "sudo" }
try {
# Update googletest submodule for linux native cmake
if ($IsLinux -or $IsOSX) {
@@ -720,10 +779,13 @@ function Start-PSBootstrap {
elseif ($IsUbuntu16) { $Deps += "libicu55" }
# Packaging tools
if ($Package) { $Deps += "ruby-dev" }
if ($Package) { $Deps += "ruby-dev", "groff" }
# Install dependencies
sudo apt-get install -y -qq $Deps
Start-NativeExecution {
Invoke-Expression "$sudo apt-get update"
Invoke-Expression "$sudo apt-get install -y -qq $Deps"
}
} elseif ($IsCentOS) {
# Build tools
$Deps += "which", "curl", "gcc-c++", "cmake", "make"
@@ -732,10 +794,12 @@ function Start-PSBootstrap {
$Deps += "libicu", "libunwind"
# Packaging tools
if ($Package) { $Deps += "ruby-devel", "rpm-build" }
if ($Package) { $Deps += "ruby-devel", "rpm-build", "groff" }
# Install dependencies
sudo yum install -y -q $Deps
Start-NativeExecution {
Invoke-Expression "$sudo yum install -y -q $Deps"
}
} elseif ($IsOSX) {
precheck 'brew' "Bootstrap dependency 'brew' not found, must install Homebrew! See http://brew.sh/"
@@ -746,55 +810,24 @@ function Start-PSBootstrap {
$Deps += "openssl"
# Install dependencies
brew install $Deps
Start-NativeExecution { brew install $Deps }
}
# Install [fpm](https://github.com/jordansissel/fpm) and [ronn](https://github.com/rtomayko/ronn)
if ($Package) {
gem install fpm ronn
}
$obtainUrl = "https://raw.githubusercontent.com/dotnet/cli/rel/1.0.0/scripts/obtain"
# Install for Linux and OS X
if ($IsLinux -or $IsOSX) {
# Uninstall all previous dotnet packages
$uninstallScript = if ($IsUbuntu) {
"dotnet-uninstall-debian-packages.sh"
} elseif ($IsOSX) {
"dotnet-uninstall-pkgs.sh"
}
if ($uninstallScript) {
curl -s $obtainUrl/uninstall/$uninstallScript -o $uninstallScript
chmod +x $uninstallScript
sudo ./$uninstallScript
} else {
Write-Warning "This script only removes prior versions of dotnet for Ubuntu 14.04 and OS X"
}
# Install new dotnet 1.0.0 preview packages
$installScript = "dotnet-install.sh"
curl -s $obtainUrl/$installScript -o $installScript
chmod +x $installScript
bash ./$installScript -c $Channel -v $Version
# .NET Core's crypto library needs brew's OpenSSL libraries added to its rpath
if ($IsOSX) {
# This is the library shipped with .NET Core
# This is allowed to fail as the user may have installed other versions of dotnet
Write-Warning ".NET Core links the incorrect OpenSSL, correcting .NET CLI libraries..."
find $env:HOME/.dotnet -name System.Security.Cryptography.Native.dylib | xargs sudo install_name_tool -add_rpath /usr/local/opt/openssl/lib
try {
# We cannot guess if the user wants to run gem install as root
Start-NativeExecution { gem install fpm ronn }
} catch {
Write-Warning "Installation of fpm and ronn gems failed! Must resolve manually."
}
}
$DotnetArguments = @{ Channel=$Channel; Version=$Version; NoSudo=$NoSudo }
Install-Dotnet @DotnetArguments
# Install for Windows
if ($IsWindows -and -not $IsCoreCLR) {
Remove-Item -ErrorAction SilentlyContinue -Recurse -Force ~\AppData\Local\Microsoft\dotnet
$installScript = "dotnet-install.ps1"
Invoke-WebRequest -Uri $obtainUrl/$installScript -OutFile $installScript
& ./$installScript -c $Channel -v $Version
if ($IsWindows) {
$machinePath = [Environment]::GetEnvironmentVariable('Path', 'MACHINE')
$newMachineEnvironmentPath = $machinePath
@@ -861,8 +894,6 @@ function Start-PSBootstrap {
}
}
} elseif ($IsWindows) {
Write-Warning "Start-PSBootstrap cannot be run in Core PowerShell on Windows (need Invoke-WebRequest!)"
}
} finally {
Pop-Location
+3
View File
@@ -0,0 +1,3 @@
logs/*.log
packages/
unstable/*/bootstrap.ps1
+120
View File
@@ -0,0 +1,120 @@
Docker
======
These Dockerfiles enable building and running PowerShell in a container for each Linux distribution we support.
There are two sets: stable and unstable.
This requires an up-to-date version of Docker, such as 1.12.
It also expects you to be able to run Docker without `sudo`.
Please follow [Docker's official instructions][install] to install `docker` correctly.
> Note: we intend to publish automatically built containers on the Docker Hub,
> but this work is still pending.
[install]: https://docs.docker.com/engine/installation/
Stable
------
The stable containers derive from the official distribution image,
such as `centos:7`, then install dependencies,
and finally install the PowerShell package.
At about 440 megabytes, they are decently minimal,
with their size being the sum of the base image (200 megabytes)
plus the uncompressed package (120 megabytes),
and about 120 megabytes of .NET Core and bootstrapping dependencies.
Unstable
--------
The unstable containers derive from their respective stable images,
such as `powershell/powershell:stable-centos7`,
then run the `bootstrap.ps1` script which clones the repository,
runs `Start-PSBootstrap -Package` to install building and packaging tools,
runs `Start-PSBuild -Crossgen` to build PowerShell with native-image DLLs,
runs `Start-PSPackage` to generate the platform's package,
and finally installs the generated package.
At about 4 gigabytes,
these images are *much* larger due to having all the tools installed.
This is intended so that the containers are useful for reproducing packages.
Automation
==========
A script, `launch.sh`, exists to automatically build all the respective images.
It has a few configuration options,
passed in as environment variables:
* `FORK`: the fork to clone in unstable builds (default: `PowerShell`)
* Set `FORK=andschwa` to clone https://github.com/andschwa/PowerShell
* `BRANCH`: the branch to checkout in unstable builds (default: `master`)
* Set `BRANCH=feature-A` to checkout the `feature-A` branch
* `BUILDS`: the type of builds to run (default: `stable unstable`)
* This is order dependent! Stable images must exist for unstable images to be built
* Set `BUILDS=stable` to run the script for just the stable images
* `DISTROS`: the distributions of Linux to run (default: `ubuntu14.04 ubuntu16.04 centos7`)
* Set `DISTROS=centos7` to build just CentOS 7 images
* Combine with `BUILDS` to filter
* `TEST`: set to 1 to `docker run Start-PSPester` inside the containers
* The containers *must* first exist; this skips the build step
* `BUILDARGS`: additional arguments to be passed to `docker build` during building
* Set `BUILDARGS="--no-cache"` to rebuild and ignore cached layers
* `RUNARGS`: additional arguments to be passed to `docker run` during testing
* Set `RUNARGS="--rm"` to automatically delete the test container when finished
For each build type (stable and unstable),
the selected distributions will run in parallel.
The output is sent to log files in `docker/logs`.
For unstable builds, the generated packages are copied to `docker/packages`.
This script is very new, and there may be bugs.
Use `set -x` to see exactly what commands it is running.
Examples
--------
To build a CentOS container with the latest released package:
```sh
$ BUILDS=stable DISTROS=centos7 ./launch.sh
Logging to docker/logs/stable-centos7.log
Waiting for stable containers to finish; tail the logs for more information.
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
powershell/powershell stable-centos7 f0a11a8009b7 20 minutes ago 438.3 MB
...
```
To generate an Ubuntu 16.04 package from andschwa's `docker` branch:
```sh
$ DISTROS=ubuntu16.04 FORK=andschwa BRANCH=docker ./launch.sh
Logging to docker/logs/stable-ubuntu16.04.log
Waiting for stable containers to finish; tail the logs for more information.
Logging to docker/logs/unstable-ubuntu16.04.log
Waiting for unstable containers to finish; tail the logs for more information.
$ ls packages
powershell_6.0.0-alpha.9-310-g5ded651-1ubuntu1.16.04.1_amd64.deb
$ tail logs/unstable-ubuntu16.04.log
Step 5 : COPY bootstrap.ps1 /
---> Using cache
---> d18a28ff1e3d
Step 6 : RUN powershell -f bootstrap.ps1 && dpkg -i PowerShell/powershell*.deb
---> Using cache
---> 9cd8b1ef79b3
Successfully built 9cd8b1ef79b3
LOG: Saving package to docker/packages
~/src/PowerShell/docker ~/src/PowerShell/docker
'/PowerShell/powershell_6.0.0-alpha.9-310-g5ded651-1ubuntu1.16.04.1_amd64.deb' -> '/mnt/powershell_6.0.0-alpha.9-310-g5ded651-1ubuntu1.16.04.1_amd64.deb'
```
+81
View File
@@ -0,0 +1,81 @@
#!/usr/bin/env bash
set -e
# This is borrowed from https://github.com/dotnet/cli/blob/18456af5caeae44defc23ad5579c838c1fda3c3d/run.sh
SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
SOURCE="$(readlink "$SOURCE")"
[[ "$SOURCE" != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
done
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
# run from directory of launch.sh so artifacts are placed appropriately
pushd "$DIR"
if [[ -z "$FORK" ]]; then
FORK=PowerShell
fi
if [[ -z "$BRANCH" ]]; then
BRANCH=master
fi
# Build both sets by default
if [[ -z "$BUILDS" ]]; then
BUILDS="stable unstable"
fi
# Build specified distributions
if [[ -z $DISTROS ]]; then
DISTROS="ubuntu14.04 ubuntu16.04 centos7"
fi
for build in $BUILDS; do
# each distro can be done in parallel; but stable must be done before unstable
for distro in $DISTROS; do
logfile="$build-$distro.log"
if [[ "$TEST" -eq 1 ]]; then logfile="test-$logfile"; fi
mkdir -p logs
logfile="logs/$logfile"
echo "Logging to docker/$logfile"
(
image="powershell/powershell:$build-$distro"
pushd "$build"
if [[ "$TEST" -eq 1 ]]; then
echo "LOG: testing $image"
command="cd PowerShell; Import-Module ./build.psm1; Install-Dotnet -NoSudo; Start-PSPester -powershell powershell -Pester ./src/Modules/Shared/Pester"
# clone repo for stable images because it's not already done
if [[ "$build" = stable ]]; then
command="git clone --recursive https://github.com/$FORK/PowerShell -b $BRANCH; $command"
fi
# run Pester tests inside container
# RUNARGS can be set in the environment
docker run $RUNARGS "$image" -c "$command"
else
echo "LOG: building $image"
# copy the common script because it lives outside the docker build context
if [[ "$build" = unstable ]]; then
cp bootstrap.ps1 "$distro"
buildargs="--build-arg fork=$FORK --build-arg branch=$BRANCH"
fi
# build and tag the image so they can be derived from
# BUILDARGS can be set in the environment
docker build $buildargs $BUILDARGS -t "$image" "$distro"
if [[ "$build" = unstable ]]; then
echo "LOG: Saving package to docker/packages"
popd
mkdir -p packages
command='cp -vf /PowerShell/powershell*{deb,rpm} /mnt 2> /dev/null'
# override entrypoint to be bash so we can use globbing
docker run --rm --volume "$(pwd)/packages:/mnt" --entrypoint bash "$image" -c "$command"
fi
fi
) &>> "$logfile" &
done
echo "Waiting for $build containers to finish; tail the logs for more information."
wait
done
popd
+36
View File
@@ -0,0 +1,36 @@
FROM centos:7
MAINTAINER Andrew Schwartzmeyer <andschwa@microsoft.com>
ARG POWERSHELL_RELEASE=v6.0.0-alpha.9
ARG POWERSHELL_PACKAGE=powershell-6.0.0_alpha.9-1.el7.centos.x86_64.rpm
# Setup the locale
ENV LANG en_US.UTF-8
ENV LC_ALL $LANG
RUN localedef --charmap=UTF-8 --inputfile=en_US $LANG
# Install dependencies and clean up
RUN yum install -y \
glibc \
libcurl \
ca-certificates \
libgcc \
libicu \
openssl \
libstdc++ \
ncurses-base \
libunwind \
uuid \
zlib \
which \
curl \
git \
&& yum clean all
# Install PowerShell package and clean up
RUN curl -SLO https://github.com/PowerShell/PowerShell/releases/download/$POWERSHELL_RELEASE/$POWERSHELL_PACKAGE \
&& yum install -y $POWERSHELL_PACKAGE \
&& rm $POWERSHELL_PACKAGE
# Use array to avoid Docker prepending /bin/sh -c
ENTRYPOINT [ "powershell" ]
+36
View File
@@ -0,0 +1,36 @@
FROM ubuntu:trusty
MAINTAINER Andrew Schwartzmeyer <andschwa@microsoft.com>
ARG POWERSHELL_RELEASE=v6.0.0-alpha.9
ARG POWERSHELL_PACKAGE=powershell_6.0.0-alpha.9-1ubuntu1.14.04.1_amd64.deb
# Setup the locale
ENV LANG en_US.UTF-8
ENV LC_ALL $LANG
RUN locale-gen $LANG && update-locale
# Install dependencies and clean up
RUN apt-get update \
&& apt-get install -y --no-install-recommends \
libc6 \
libcurl3 \
ca-certificates \
libgcc1 \
libicu52 \
libssl1.0.0 \
libstdc++6 \
libtinfo5 \
libunwind8 \
libuuid1 \
zlib1g \
curl \
git \
&& rm -rf /var/lib/apt/lists/*
# Install PowerShell package and clean up
RUN curl -SLO https://github.com/PowerShell/PowerShell/releases/download/$POWERSHELL_RELEASE/$POWERSHELL_PACKAGE \
&& dpkg -i $POWERSHELL_PACKAGE \
&& rm $POWERSHELL_PACKAGE
# Use array to avoid Docker prepending /bin/sh -c
ENTRYPOINT [ "powershell" ]
+36
View File
@@ -0,0 +1,36 @@
FROM ubuntu:xenial
MAINTAINER Andrew Schwartzmeyer <andschwa@microsoft.com>
ARG POWERSHELL_RELEASE=v6.0.0-alpha.9
ARG POWERSHELL_PACKAGE=powershell_6.0.0-alpha.9-1ubuntu1.16.04.1_amd64.deb
# Setup the locale
ENV LANG en_US.UTF-8
ENV LC_ALL $LANG
RUN locale-gen $LANG && update-locale
# Install dependencies and clean up
RUN apt-get update \
&& apt-get install -y --no-install-recommends \
libc6 \
libcurl3 \
ca-certificates \
libgcc1 \
libicu55 \
libssl1.0.0 \
libstdc++6 \
libtinfo5 \
libunwind8 \
libuuid1 \
zlib1g \
curl \
git \
&& rm -rf /var/lib/apt/lists/*
# Install PowerShell package and clean up
RUN curl -SLO https://github.com/PowerShell/PowerShell/releases/download/$POWERSHELL_RELEASE/$POWERSHELL_PACKAGE \
&& dpkg -i $POWERSHELL_PACKAGE \
&& rm $POWERSHELL_PACKAGE
# Use array to avoid Docker prepending /bin/sh -c
ENTRYPOINT [ "powershell" ]
+8
View File
@@ -0,0 +1,8 @@
# This is intended to be used inside Docker containers
git clone --recursive https://github.com/$env:fork/PowerShell.git -b $env:branch
Set-Location PowerShell
Import-Module ./build.psm1
Start-PSBootstrap -Package -NoSudo
Start-PSBuild -Crossgen
Start-PSPackage
+9
View File
@@ -0,0 +1,9 @@
FROM powershell/powershell:stable-centos7
MAINTAINER Andrew Schwartzmeyer <andschwa@microsoft.com>
ARG fork=PowerShell
ARG branch=master
COPY bootstrap.ps1 /
RUN powershell -f bootstrap.ps1 \
&& yum install -y PowerShell/powershell*.rpm
+9
View File
@@ -0,0 +1,9 @@
FROM powershell/powershell:stable-ubuntu14.04
MAINTAINER Andrew Schwartzmeyer <andschwa@microsoft.com>
ARG fork=PowerShell
ARG branch=master
COPY bootstrap.ps1 /
RUN powershell -f bootstrap.ps1 \
&& dpkg -i PowerShell/powershell*.deb
+9
View File
@@ -0,0 +1,9 @@
FROM powershell/powershell:stable-ubuntu16.04
MAINTAINER Andrew Schwartzmeyer <andschwa@microsoft.com>
ARG fork=PowerShell
ARG branch=master
COPY bootstrap.ps1 /
RUN powershell -f bootstrap.ps1 \
&& dpkg -i PowerShell/powershell*.deb
-22
View File
@@ -1,22 +0,0 @@
Docker
======
Build
=====
If you want to build it yourself:
```
docker build -t powershell .
```
Run
===
After you have built using Docker, running PowerShell very simple:
```
docker run -it powershell
```
This Docker image is based on Ubuntu 16.04, and follows the instructions from the [Linux Installation docs][u16].
[u16]: linux.md#ubuntu-1604
@@ -11,15 +11,7 @@
//! @brief GetFullyQualifiedName retrieves the fully qualified dns name of the host
//!
//! @exception errno Passes these errors via errno to GetLastError:
//! - ERROR_INVALID_FUNCTION: getlogin_r() returned an unrecognized error code (from GetComputerName)
//! - ERROR_INVALID_ADDRESS: buffer is an invalid address (from GetComputerName)
//! - ERROR_GEN_FAILURE: buffer not large enough (from GetComputerName)
//! - ERROR_BAD_NET_NAME: Cannot determine network short name
//!
//! @retval username as UTF-8 string, or null if unsuccessful
//!
char *GetFullyQualifiedName()
{
errno = 0;
@@ -43,17 +35,18 @@ char *GetFullyQualifiedName()
* POSIX compliant, and some are being deprecated. getaddrinfo seems
* to be the best choice.
*/
char *fullName = NULL;
if (getaddrinfo(computerName, "http", &hints, &info) != 0)
{
errno = ERROR_BAD_NET_NAME;
return NULL;
goto exit;
}
// info is actually a link-list. We'll just return the first full name
char *fullName = strndup(info->ai_canonname, strlen(info->ai_canonname));
// return the first canonical name in the list
fullName = strndup(info->ai_canonname, strlen(info->ai_canonname));
// only free info if getaddrinfo was successful
freeaddrinfo(info);
exit:
free(computerName);
return fullName;
}
@@ -12,19 +12,30 @@
TEST(GetFullyQualifiedNameTest, ValidateLinuxGetFullyQualifiedDomainName)
{
std::string actual(GetFullyQualifiedName());
std::string hostname(GetComputerName());
char *hostname = GetComputerName();
ASSERT_STRNE(NULL, hostname);
// this might be fail
errno = 0;
char *actual = GetFullyQualifiedName();
int fqdnErrno = errno;
struct addrinfo hints, *info;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_CANONNAME;
EXPECT_FALSE(getaddrinfo(hostname.c_str(), "http", &hints, &info));
errno = 0;
if (getaddrinfo(hostname, "http", &hints, &info) != 0)
{
// test that getaddrinfo failed the same way
EXPECT_EQ(fqdnErrno, errno);
goto exit;
}
// Compare canonical name to FQDN
EXPECT_STREQ(info->ai_canonname, actual.c_str());
EXPECT_STREQ(info->ai_canonname, actual);
freeaddrinfo(info);
exit:
free(hostname);
}